String number

Purpose: Convert string to number.

 string-number <string> [ to <number> ] \
     [ base <base> ] \
     [ status <status> ]

<string> is converted to <number> in "to" clause, using <base> in "base" clause, where <base> is by default either 10, or 16 (if number is prefixed with "0x" or "0X", excluding any leading minus or plus sign) or 8 (if number is prefixed with "0", excluding any leading minus or plus sign).

<base> can be between 2 and 36, inclusive. <number> can be positive or negative (i.e. signed) and can be up to 64-bit in length. If <base> is 0, it is the same as if it is not specified, i.e. default behavior applies.

<status> number (in "status" clause) is GG_OKAY if conversion was successful. If it wasn't successful, <number> is 0 and <status> is GG_ERR_OVERFLOW if <string> represents a number that requires over 64 bits of storage, GG_ERR_INVALID if <base> is incorrect, GG_ERR_EXIST if <string> is empty or no digits specified.

If there are trailing invalid characters (for instance "182xy" for base 10), <number> is the result of conversion up to the first invalid character and <status> is GG_ERR_TOO_MANY. In this example, <number> would be 182.
Shortcut
For convenience, you can use a shortcut for converting a string to a number, by prepending a "#" sign to the variable name, for example:
 set-string s = "-10"
 set-number n = #s + 10

The above is the same as:
 set-string s = "-10"
 string-number s to val
 set-number n = val + 10

Effectively, # in front of a string variable is the same as string-number that converts it to a number with the base being the default (see above). If a string cannot be converted to a number, your program will error out. To check values with a status, use string-number.

You can use # for an expression, in which case the expression must be within parenthesis:
 set-string s1 = "-10"
 set-string s2 = "2"
 set-number n = #(s1+s2) + 10

In this case number "n" will have value of -92, because strings "s1" and "s2" concatenate to produce "-102", which is then converted to a number and finally 10 added to it, producing -92.
Examples
In this example, number "n" would be 49 and status "st" would be GG_OKAY:
 string-number "49" to n base 10 status st



Copyright (c) 2019-2025 Gliim LLC. All contents on this web site is "AS IS" without warranties or guarantees of any kind.