浮点十六进制字符串返回浮点数

hyo*_*kim 4 python hex python-2.7 python-3.x

我想将浮点数更改为十六进制,将十六进制更改为浮点数.

所以我想得到第一个浮点数和浮点数的末尾相等.

这是我在Python 2.7上尝试过的; f1= first float,h= hex string ,=十六进制字符串中的f2新浮点值.

f1 = 15.3
h=f1.hex() #0x1.e99999999999ap+3
Run Code Online (Sandbox Code Playgroud)

如何将h十六进制字符串再次更改为浮点数?

我试过了

f2 = int(h,16)
f2 = float(h)
f2 = h.float()
Run Code Online (Sandbox Code Playgroud)

但这些都不起作用.

如果有帮助,我也可以使用Python 3.5.

Mar*_*ers 6

使用float.fromhex()classmethod:

>>> float.fromhex(h)
15.3
Run Code Online (Sandbox Code Playgroud)

这是float.hex()实例方法的直接反转.