Nad*_*dim 2 python hex byte integer python-3.x
我在 python 中有一个函数,它读取串行端口缓冲区中的前 3 个字节。然后我想将第三个字节转换为整数,这将使我能够确定总字节数组的长度。但是,当我使用时,int()出现以下错误:
ValueError: invalid literal for int() with base 16: b'\x16'
我尝试进一步切割字符串,但只返回 b''。如何将字节转换为整数?
谢谢你!
使用int.from_bytes()。
>>>
int.from_bytes(b'\x00\x10', byteorder='big')
16>>>
int.from_bytes(b'\x00\x10', byteorder='little')
4096>>>
int.from_bytes(b'\xfc\x00', byteorder='big', signed=True)
-1024>>>
int.from_bytes(b'\xfc\x00', byteorder='big', signed=False)
64512>>>
int.from_bytes([255, 0, 0], byteorder='big')
16711680