0

我从 bytearray 中的网络获取数据,我需要在 bytearray[i] 中获取整数值。它是 ASCII,当我尝试用 int() 转换整数时出现异常。如何尝试在 python 中将 '\x01' 转换为 1?谢谢。

4

1 回答 1

0

使用ord

>>> ord('\x01')
1

struct.unpack

>>> import struct
>>> struct.unpack('B', '\x01')
(1,)
>>> struct.unpack('2B', '\x01\x02')
(1, 2)
于 2013-09-07T15:30:42.083 回答