Convert signed IEEE 754 float to hexadecimal representation

前端 未结 3 1796
萌比男神i
萌比男神i 2021-01-18 12:40

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

3条回答
  •  抹茶落季
    2021-01-18 12:58

    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
    

提交回复
热议问题