By default, the number base used for integer number conversion is
given by the contents of the variable base. Base
affects both input integer conversion (unless a number prefix is used,
see Literals in source code) and output integer conversion (unless
a base-specific word such as h. or dec. is used,
see Simple numeric output, see Integer to string conversion). In
Gforth, non-decimal base disables floating-point conversion.
Note that a lot of confusion can result from unexpected values of
base. If you change base anywhere, make sure to save
the old value and restore it afterwards; better yet, use
base-execute, which does this for you.
In general I recommend keeping base decimal; For dealing with
the popular non-decimal bases, use number prefixes for inputting them
(see Literals in source code) and h. and
base-execute for outputting them.
execute xt with the content of BASE being u, and
restoring the original BASE afterwards.
User variable – a-addr is the address of a cell that
stores the number base used by default for number conversion during
input and output. Don’t store to base, use
base-execute instead.
Set base to $10 (hexadecimal). In many cases
base-execute is a better alternative.
Set base to #10 (decimal). In many cases
base-execute is a better alternative.
User variable – a-addr is the address of a cell that stores
the position of the decimal point in the most recent input integer
conversion. After the conversion of a number containing no decimal
point, dpl is -1. After the conversion of 2341239. it
holds 0. After the conversion of 234123.9 it contains 1, and so
forth.
Number conversion has a number of traps for the unwary:
base is non-decimal.
base @ . – the number base is always 10 in the current number
base. Instead, use something like base @ dec.
bin but it does not set the number base!
(see General files).
. of a double-precision number to be the
final character in the string. Gforth allows the . to be
anywhere.
Words affected by base are rec-number (see Default recognizers), >number (see String to number conversion),
and the words described in the sections Simple numeric output,
and Integer to string conversion. Rec-number sets
dpl.