Convert signed IEEE 754 float to hexadecimal representation
Asked Answered
B

3

7

I'm using a front-end of Lua which is unfortunately outdated, so I'm stuck with version 5.1 here, meaning the bit32 library is out of reach (which I probably could have used to convert this).

So I'm wondering if anyone knows of a way I could implement either a floating-point to binary (digits) function or, better yet, floating-point to hex. The best I've been able to come up with so far is a decimal to binary/hex function...

Bimetallic answered 19/9, 2013 at 4:55 Comment(8)
There is a backport of bit32 for Lua 5.1.Sturges
Try also inf.puc-rio.br/~roberto/struct or tecgraf.puc-rio.br/~lhf/ftp/lua/#lpack.Sturges
Could you point me in the direction to the bit32 backport?Bimetallic
See lua-users.org/lists/lua-l/2011-01/msg01091.html.Sturges
Even without a bit library you can do that rather efficiently. Maybe reading the source of lua-MessagePack could help you: github.com/fperrad/lua-MessagePack/blob/master/src/…Quasar
Thanks for the link, catwell, I took a snippet of that source code and I've got a function for it now! :-)Bimetallic
@Bimetallic Would you mind posting your solution as an answer (and accept it), so that it will be useful for other people browsing this thread? Thanks.Adjutant
@LorenzoDonati it has been done.Bimetallic
B
4

The following functions uses some code from François Perrad's lua-MessagePack. A big thank you goes to him.

function float2hex (n)
    if n == 0.0 then return 0.0 end

    local sign = 0
    if n < 0.0 then
        sign = 0x80
        n = -n
    end

    local mant, expo = math.frexp(n)
    local hext = {}

    if mant ~= mant then
        hext[#hext+1] = string.char(0xFF, 0x88, 0x00, 0x00)

    elseif mant == math.huge or expo > 0x80 then
        if sign == 0 then
            hext[#hext+1] = string.char(0x7F, 0x80, 0x00, 0x00)
        else
            hext[#hext+1] = string.char(0xFF, 0x80, 0x00, 0x00)
        end

    elseif (mant == 0.0 and expo == 0) or expo < -0x7E then
        hext[#hext+1] = string.char(sign, 0x00, 0x00, 0x00)

    else
        expo = expo + 0x7E
        mant = (mant * 2.0 - 1.0) * math.ldexp(0.5, 24)
        hext[#hext+1] = string.char(sign + math.floor(expo / 0x2),
                                    (expo % 0x2) * 0x80 + math.floor(mant / 0x10000),
                                    math.floor(mant / 0x100) % 0x100,
                                    mant % 0x100)
    end

    return tonumber(string.gsub(table.concat(hext),"(.)",
                                function (c) return string.format("%02X%s",string.byte(c),"") end), 16)
end


function hex2float (c)
    if c == 0 then return 0.0 end
    local c = string.gsub(string.format("%X", c),"(..)",function (x) return string.char(tonumber(x, 16)) end)
    local b1,b2,b3,b4 = string.byte(c, 1, 4)
    local sign = b1 > 0x7F
    local expo = (b1 % 0x80) * 0x2 + math.floor(b2 / 0x80)
    local mant = ((b2 % 0x80) * 0x100 + b3) * 0x100 + b4

    if sign then
        sign = -1
    else
        sign = 1
    end

    local n

    if mant == 0 and expo == 0 then
        n = sign * 0.0
    elseif expo == 0xFF then
        if mant == 0 then
            n = sign * math.huge
        else
            n = 0.0/0.0
        end
    else
        n = sign * math.ldexp(1.0 + mant / 0x800000, expo - 0x7F)
    end

    return n
end
Bimetallic answered 15/11, 2013 at 8:43 Comment(0)
D
1

The float2hex in the example above returns an int. That being said if anyone needs it here is a intToHex conversion function that can be found in the lua archives (http://lua-users.org/lists/lua-l/2004-09/msg00054.html). I used the return value of the float2hex function above and fed it into this function. The output of intToHex is a string (Ex: 0xA4CD).

function intToHex(IN)
    local B,K,OUT,I,D=16,"0123456789ABCDEF","",0
    while IN>0 do
        I=I+1
        IN,D=math.floor(IN/B),math.mod(IN,B)+1
        OUT=string.sub(K,D,D)..OUT
    end


    OUT = "0x" .. OUT
    return OUT
end
Disaffiliate answered 16/6, 2017 at 14:8 Comment(0)
S
0

The issue I faced was trying to convert from the Hex back to a Float; You can use that tonumber(x, 16) to convert it in combination with "Error - Syntactical Remorse's" response. The string.sub is only there because my Modbus driver does not swap the bytes :)

function convertFloatSwapWord(number) -- number is string of format = 0x########
     local x = 0
     x = float2hex(number)
     x = intToHex(x)
     x = string.sub(x,5,6) .. string.sub(x,3,4) .. string.sub(x,9,10) .. string.sub(x,7,8)
     x = hex2float(tonumber(x, 16))
     return x
end
Sporades answered 12/3, 2018 at 19:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.