Code: Select all
(int "1111" 0 2) → 15 ; base 2 conversion
(int "0FF" 0 16) → 255 ; base 16 conversion
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)
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?