-v0.1 2021.7.8 Hans draft
参考文章:https://blog.csdn.net/and_zj/article/details/67001655
上述文章使用的是 sruct 模块,在移远quecpython中使用的ustruct 来替代:
import ustruct
def bytesToFloat(h1,h2,h3,h4):
ba = bytearray()
ba.append(h1)
ba.append(h2)
ba.append(h3)
ba.append(h4)
return ustruct.unpack("!f",ba)[0]
def floatToBytes(f):
bs = ustruct.pack("f",f)
return (bs[3],bs[2],bs[1],bs[0])
if __name__ == "__main__":
f = 1.0
b = floatToBytes(f)
print("b="+str(b))
f2 = bytesToFloat(b[0],b[1],b[2],b[3])
print("f2="+str(f2))
f3 = bytesToFloat(0x3f,0x80,0x00,0x00)
print("f3="+str(f3))
结果如下:
>>> example.exec('usr/bytes_Float.py')
b=(63, 128, 0, 0)
f2=1.0
f3=1.0
>>>