一,说明:
1,传递参数需要是16进制格式的字符串,或者数字(不支持负数)
2,只支持32位,单精度
3,提供一个验证使用的网址https://www.h-schmidt.net/FloatConverter/IEEE754.html
4,Hex转浮点数 代码如下:
--备注:math.modf() 这个函数就是为了获取除法的整数部分
function hexToFloat( hexString )
if hexString == nil then
return 0
end
local t = type( hexString )
if t == "string" then
hexString = tonumber(hexString , 16)
end
local hexNums = hexString
local sign = math.modf(hexNums/(2^31))
local exponent = hexNums % (2^31)
exponent = math.modf(exponent/(2^23)) -127
local mantissa = hexNums % (2^23)
for i=1,23 do
mantissa = mantissa / 2
end
mantissa = 1+mantissa
-- print(mantissa)
local result = (-1)^sign * mantissa * 2^exponent
return result
end
<