def byte2float(x):
return struct.unpack('=d', x)[0]
def float2byte(f):
return [hex(i) for i in struct.pack('f', f)]
def int2byte(f):
return struct.pack('l', f) # b'\xa0\xb3\xc3=\x0c\x00\x00\x00'
def new_float2byte(f):
return struct.pack('d', f)
def byte2int(x):
return struct.unpack('l', x)[0]
int is 35871268326178452
b'\x94~9~\xbap\x7f\x00' <int to byte>
2.7982836490718717e-306 <byte to float>
b'\x94~9~\xbap\x7f\x00' <float to byte>
35871268326178452 <byte to int>
下面是找的其他参考资料,大家可以看看。
floating point - How to convert a byte array to float in Python - Stack Overflow
casting - How to interpret 4 bytes as a 32-bit float using Python - Stack Overflowhttps://stackoverflow.com/questions/37093485/how-to-interpret-4-bytes-as-a-32-bit-float-using-python/37093610Python中struct.pack()和struct.unpack()用法详细说明_把握自己。-CSDN博客
python3 struct模块 处理二进制 pack unpack用法_whatday的专栏-CSDN博客_python3 struct.unpack
python - Convert Bytes to Floating Point Numbers? - Stack Overflow
Python:struct模块的pack、unpack - 何永灿 - 博客园
|