Page 1 of 1

xml.lua - Added mod to parse hex strings as a number

Posted: Mon Feb 23, 2009 7:10 am
by zer0
I added a mod to change parsed hex strings to a number. Handy if you want to put hexadecimal as a value.

For example:

Code: Select all

<config name="mem_offset">0x0f00</config>
Becomes 1024.

In XML.lua:

Here was the mod.

Code: Select all

  -- convert hex string to number.  
  if( string.find(data, "^(0x%x+)$") ~= nil) then
    --print ("data: ", data)
    return tonumber(data:sub(3), 16);
  end
Added to the implicitCast function like so:

Code: Select all

function implicitCast(data)
  if( data == "true" ) then
    return true; end;

  if( data == "false" ) then
    return false; end;
  if( string.find(data, "[^%.?0-9+]") == nil ) then
    return tonumber(data);
  end
  -- convert hex string to number.  
  if( string.find(data, "^(0x%x+)$") ~= nil) then
    --print ("data: ", data)
    return tonumber(data:sub(3), 16);
  end
  return data;
end
I have several uses for it so I think it's pretty useful to include it in the XML lua parser.

Re: xml.lua - Added mod to parse hex strings as a number

Posted: Mon Feb 23, 2009 2:25 pm
by Administrator
Good idea, I've just added this to MicroMacro 1.0, and it will appear in the next beta or full release. Thanks.

Re: xml.lua - Added mod to parse hex strings as a number

Posted: Tue Feb 24, 2009 6:48 am
by zer0
Hehe, awesome thanks Elv. :D