In what situations do you use these conversions?
hexnumber = '3xz'
--A
tonum(hexnumber,1) : 768 (0x0300.0000)
--B
tonum("0x"..hexnumber,4) : 0 (0x0000.0000)
--C
tonum('0x'..hexnumber) : -- NO VALUE --([nil]) |
Example
I want "nil" to be returned when I use a character as an identifier. Therefore, I use the "C" conversion.
hexstr = '108000789$'
hexval = TONORM(hexstr) -- Normalize the value [number boolian nil], otherwise it remains a string.
hextable = {}
if type(hexval) == 'string' then
foreach(split(hexstr, 3), function(v)
add(hextable, tonum('0x' .. v))
end)
end
-- Handle hexadecimal conversions without being converted to numbers by normalization.
-- Only the value of number is stored in the hextable.
-- "$" is not stored.
|
hi there! I don’t fully follow what you’re trying to do.
in your example, hexnumber is strange because it contains a byte and a half, not an even number of hex digits.
pico8’s functions convert between number and string. tostr(142,0x1) gives "0x008e.0000", which can be cut with sub to get "008e" to store somewhere, then later tonum("008e",0x1) gives us back 142.
the trick with tonum("0x"..bytes) is not needed anymore thanks to the new flags you can pass to tonum. I don’t understand the $ sign or why your hex strings are not even.
hope this helps!
Hi @merwok
Thank you for your help!
I kind of regret why I went with this example! :P
> tostr(142,0x1) -> tonum("008e",0x1) -> 142
Yes, unless you have a specific reason, I think this method is simply sufficient.
However, this example was intended to store data in 12 bits, not in the delimited format of one byte.
hexstr = '108000789$' -- input
hextable = { 0x108 , 0x000 , 0x789 , nil } -- output |
I wanted to show that we are using tonum("0x". .bytes), which has a different behavior.
[Please log in to post a comment]



