Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hex constant = malformed number?

Tags:

lua

I have a Lua script, where I'm trying to use hex numbers (0x..). If I run this script in the console, with the official Windows binaries, it works fine. But if I run it in my application (simple dofile), I get

malformed number near '0x1F'

It doesn't matter what the hex is, I always get that error, as if it wouldn't support them. The library I'm using is Lua 5.1.4, and I've tried 2 different ones (the first one being one I've compiled myself), so that shouldn't be the problem.

Does anyone have a clue what might be wrong here?

Edit: It's not the script. No matter what I do, a simple "foo = 0xf" already triggers the error, even if there's nothing else in the file.

Update:

tonumber("0xf")

This returns nil, while

tonumber("15")

work fine. There's definitely something wrong with hex in my libs...

like image 652
Mars Avatar asked Feb 16 '12 11:02

Mars


2 Answers

If hex literals aren't working for you (though they should), you can always use hex from lua by doing tonumber("fe",16)

like image 67
daurnimator Avatar answered Nov 15 '22 10:11

daurnimator


Why do functions have to be different in different compilers, ...why?

Alright, the problem was that Lua tries to convert numbers into double by default. For this it uses the function "strtod", which takes 2 arguments, the string, and a char pointer. The char pointer is supposed to point to the last position after the parsed number. Which for a hex number would mean the 'x', after the '0'. If this isn't the case, Lua assumes an error, and gives us this nice little error message.

I've compiled Lua using DMC, because I need the lib to be in OMF, and I assume others used DMC as well. But apparently DMC's strtod works differenty, since the pointers always point to the start of the string if it's a hex... or rather any invalid number.

I've now added a little hack, which checks for the x, if conversion to double failed. Not pretty, but it works fine for now.

int luaO_str2d (const char *s, lua_Number *result) {
  char *endptr;
  *result = lua_str2number(s, &endptr);

  /* Hack for DMC */
  if (endptr == s)
    if(*(s+1) == 'x' || *(s+1) == 'X')
      endptr++;
    else
      return 0; /* conversion failed */
like image 31
Mars Avatar answered Nov 15 '22 10:11

Mars