This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
lincore's profile picture

[1.4.6|FTB] tonumber("_", 16) == 8

Started by lincore, 18 December 2012 - 10:23 PM
lincore #1
Posted 18 December 2012 - 11:23 PM
I've been trying to convert strings of characters [0-9A-Fa-f_], each to either a decimal number or false if NaN.
The expression tonumber("_", 16) or false returns 8, however.

I've tested this on the integrated server - in my script as well as in the built-in interactive interpreter to eliminate the possibility of side effects. I also tested this quirk in an external interpreter for lua 5.1.4, where the result was - as expected - false. However, I'm not sure if that means that this behaviour is indeed related to CC nor do I know if it's a bug or actually a feature I am unaware of.

To reproduce this behaviour, type tonumber("_", 16) in the interactive interpreter.
PixelToast #2
Posted 19 December 2012 - 03:02 AM
tonumber only supports chars from 0-9 and A-Z

http://www.lua.org/m...ml#pdf-tonumber
The base may be any integer between 2 and 36, inclusive.
so im guessing it wraps around?
i dont know

also something interesting:
you can compare strings
"A">"B"
its awesome
Cloudy #3
Posted 19 December 2012 - 03:37 AM
Most likely a LuaJ - not much can be done about them really - we do plan to move away from LuaJ though. You'll probably have to deal with it for now.
Lyqyd #4
Posted 19 December 2012 - 04:47 AM
As a workaround for this specific case, try:

chr ~= "_" and tonumber(chr, 16) or false