我有一个二进制文件,我必须解析,我正在使用Python.有没有办法取4个字节并将其转换为单个精确浮点数?
最佳答案
>>> import struct
>>> struct.pack('f', 3.141592654)
b'\xdb\[email protected]'
>>> struct.unpack('f', b'\xdb\[email protected]')
(3.1415927410125732,)
>>> struct.pack('4f', 1.0, 2.0, 3.0, 4.0)
'\x00\x00\x80?\x00\x00\[email protected]\x00\[email protected]@\x00\x00\[email protected]'