开发者

Arbitrary length bytes to int builtin in Python [duplicate]

开发者 https://www.devze.com 2023-02-19 23:20 出处:网络
This question already has answers here: How to convert a string of bytes into an int? 开发者_开发技巧(12 answers)
This question already has answers here: How to convert a string of bytes into an int? 开发者_开发技巧 (12 answers) Closed 9 years ago.

I'm looking for a function that takes an arbitrary length bytes object, and converts it to an int. Obviously endianness is a required parameter to this function.

I'm certain I encountered a builtin on either bytes or int, but can't find it anymore. There are plenty of answers on similar questions involving use of struct, and manually enumerating the individual byte values. Is there a builtin that does this conversion without using C-like assumptions/modules?

def int(bytes, 'little') -> int


Since 3.2:

>>> int.from_bytes(b'\xFF\x00','little')
255
>>> int.from_bytes(b'\xFF\x00','big')
65280
0

精彩评论

暂无评论...
验证码 换一张
取 消