Page 1 of 1

(int) expression must start with a digit?

Posted: Fri Jun 05, 2009 5:33 pm
by m35
I see in the manual these examples for the (int) function.

Code: Select all

(int "1111" 0 2)  → 15   ; base 2 conversion
(int "0FF" 0 16)  → 255  ; base 16 conversion
Trying some of my own variations:

Code: Select all

> (int "ff")
nil
> (int "0xff")
255
> (int "0ff")
0
> (int "ff" 'err 16)
err
> (int "0xff" 'err 16)
255
> (int "0ff" 'err 16)
255
> (int "-0ff" 'err 16)
-255
> (int "-ff" 'err 16)
err
> (int "-ff" 'err 16)
So it seems the expression must always start with a digit.

I'm trying to parse a lot of hex values that look like "ffff" or "-ffff".

I suppose I can append a 0 or 0x in front of the expression if it's positive, or insert a 0 or 0x after the '-' if it's negative, but this seems like an annoyance.

If the base is supplied, couldn't it work even if there is no leading digit?

Posted: Fri Jun 05, 2009 6:23 pm
by Lutz
They will all work without the leading zero in version 10.0.8

Code: Select all

> (int "ff" 'err 16)
255
> (int "-ff" 'err 16)
-255
> (int "+ff" 'err 16)
255
> 

Posted: Fri Jun 05, 2009 6:26 pm
by m35
Cool, thank Lutz :D