www.delorie.com/gnu/docs/gforth/gforth_121.html   search  
Buy GNU books!

Gforth Manual

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.13.2 Number Conversion

This section describes the rules that the text interpreter uses when it tries to convert a string into a number.

Let <digit> represent any character that is a legal digit in the current number base(28).

Let <decimal digit> represent any character in the range 0-9.

Let {a b} represent the optional presence of any of the characters in the braces (a or b or neither).

Let * represent any number of instances of the previous character (including none).

Let any other character represent itself.

Now, the conversion rules are:

By default, the number base used for integer number conversion is given by the contents of the variable base. 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. In general I recommend keeping base decimal, and using the prefixes described below for the popular non-decimal bases.

doc-dpl doc-base doc-hex doc-decimal

Gforth allows you to override the value of base by using a prefix(29) before the first digit of an (integer) number. Four prefixes are supported:

Here are some examples, with the equivalent decimal number shown after in braces:

-$41 (-65), %1001101 (205), %1001.0001 (145 - a double-precision number), 'AB (16706; ascii A is 65, ascii B is 66, number is 65*256 + 66), 'ab (24930; ascii a is 97, ascii B is 98, number is 97*256 + 98), &905 (905), $abc (2478), $ABC (2478).

Number conversion has a number of traps for the unwary:

You can read numbers into your programs with the words described in 5.18.5 Input.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003