在Python中内置的int的任意长度字节

Mat*_*ner 8 python int byte endianness python-3.x

我正在寻找一个采用任意长度 bytes对象的函数,并将其转换为int.显然,字节顺序是此函数的必需参数.

我敢肯定,我遇到了无论是内置bytesint,但无法找到它了.关于使用struct和手动枚举各个字节值的类似问题,有很多答案.有没有使用类似C的假设/模块进行转换的内置函数?

def int(bytes, 'little') -> int
Run Code Online (Sandbox Code Playgroud)

Kab*_*bie 17

从3.2开始:

>>> int.from_bytes(b'\xFF\x00','little')
255
>>> int.from_bytes(b'\xFF\x00','big')
65280
Run Code Online (Sandbox Code Playgroud)

  • 拥有python2版本也会很不错. (3认同)