Convert signed IEEE 754 float to hexadecimal representation

前端 未结 3 1799
萌比男神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条回答
  •  -上瘾入骨i
    2021-01-18 13:08

    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
    

提交回复
热议问题