将签署的IEEE 754浮点数转换为十六进制表示形式 [英] Convert signed IEEE 754 float to hexadecimal representation
问题描述
我正在使用Lua的一个前端,不幸的是这个前端已经过时,所以我在这里遇到了版本5.1,这意味着 bit32
库不可用我可能可以用它来转换)。
所以我想知道是否有人知道我可以实现一个浮点到二进制(数字)函数,或者更好,浮点到十六进制。到目前为止,我所能想到的最好的是十进制二进制/十六进制函数...
以下函数使用来自FrançoisPerrad的 lua-MessagePack
的一些代码。
pre $函数float2hex(n)
if n == 0.0然后返回0.0结束
本地标志= 0
如果n < 0.0 then
sign = 0x80
n = -n
end
本地mant,expo = math.frexp(n)
local hext = {}
如果mant〜= mant然后
hext [#hext + 1] = string.char(0xFF,0x88,0x00,0x00)
elseif mant ==数学.huge或expo> 0x80然后
if sign == 0 then
hext [#hext + 1] = string.char(0x7F,0x80,0x00,0x00)
else
hext [#hext + 1] = string.char(0xFF,0x80,0x00,0x00)
end
$ b elseif(mant == 0.0 and expo == 0)or expo< -0x7E然后
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
返回字符串(string.gsub(table.concat(hext),(。)),
函数(c)返回string.format(%02X%s,string.byte(c), )end)16)
end
函数hex2float(c)
if c == 0然后返回0.0结束
local c = string。 gsub(string.format(%X,c),(..),函数(x)返回string.char(tonumber(x,16))end)
local b1,b2,b3, b4 = string.byte(c,1,4)
local sign = b1> (b2%0x80)* 0x100 + b3)* 0x100 + b4
本地expo =(b1%0x80)* 0x2 +
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
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...
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
这篇关于将签署的IEEE 754浮点数转换为十六进制表示形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!