我想在 python 中执行从 32 位二进制到浮点数的 IEEE 754 转换。
我试过这个
import struct
f = int('11000001101011000111101011100001', 2)
print struct.unpack('f', struct.pack('i', f))[0]
Run Code Online (Sandbox Code Playgroud)
但这不适用于带有负符号位的数字。
预期的输出应该是这样的:
bintofloat(11000001101011000111101011100001)
>>> -21.56
Run Code Online (Sandbox Code Playgroud)