我正在使用Lua的前端,不幸的是它已经过时了,所以我在这里停留在5.1版,这意味着该bit32
库太遥不可及(我可能可以用来转换它)。
因此,我想知道是否有人知道我可以实现将浮点数转换为二进制(数字)功能,或者更好地将浮点数转换为十六进制的方法。到目前为止,我能提出的最好的办法是将十进制转换为二进制/十六进制函数...
以下函数使用FrançoisPerrad的一些代码lua-MessagePack
。非常感谢您。
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句