tags:

views:

223

answers:

3

Lua's tonumber function is nice but can only convert unsigned integers unless they are base 10. I have a situation where I have numbers like 01.4C that I would like to convert to decimal.

I have a crummy solution:

function split(str, pat)
   local t = {} 
   local fpat = "(.-)" .. pat
   local last_end = 1
   local s, e, cap = str:find(fpat, 1)
   while s do
      if s ~= 1 or cap ~= "" then
        table.insert(t,cap)
      end
      last_end = e+1
      s, e, cap = str:find(fpat, last_end)
   end
   if last_end <= #str then
      cap = str:sub(last_end)
      table.insert(t, cap)
   end
   return t
end
-- taken from http://lua-users.org/wiki/SplitJoin

function hex2dec(hexnum)
  local parts = split(hexnum, "[\.]")
  local sigpart = parts[1]
  local decpart = parts[2]

  sigpart = tonumber(sigpart, 16)
  decpart = tonumber(decpart, 16) / 256

  return sigpart + decpart
end

print(hex2dec("01.4C")) -- output: 1.296875

I'd be interested in a better solution for this if there is one.

+3  A: 

Move the "heximal" point two places to the right, convert to decimal, and then divide by 256.

014C  ==>  332 / 256 = 1.296875
Robert Harvey
+4  A: 

If your Lua is compiled with a C99 compiler (or maybe earlier gcc), then...

~ e$ lua
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> return tonumber"0x01.4C"
1.296875
Doug Currie
Is this documented in the Lua documentation? This is so simple I feel dumb for missing it...
Zack Mulgrew
It is not documented, and is implementation/configuration dependent. In your luaconf.h there is a #define lua_str2number(s,p) strtod((s), (p)) -- so it all depends on the C library implementation of strtod or whatever you use for lua_str2number in luaconf.h
Doug Currie
Good to know. Regardless, this is what I'll be using. Thanks!
Zack Mulgrew
+7  A: 

Here is a simpler solution:

function hex2dec(hexnum)
        local a,b=string.match(hexnum,"(.*)%.(.*)$")
        local n=#b
        a=tonumber(a,16)
        b=tonumber(b,16)
        return a+b/(16^n)
end

print(hex2dec("01.4C")) -- output: 1.296875
lhf
+1 This is a nice solution but for my particular case I found Doug's answer to be the most straightforward. For platforms other than the one I'm currently on this solution would be what I would use. Thanks!
Zack Mulgrew