Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arbitrary length bytes to int builtin in Python [duplicate]

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
like image 648
Matt Joiner Avatar asked Mar 28 '11 02:03

Matt Joiner


Video Answer


1 Answers

Since 3.2:

>>> int.from_bytes(b'\xFF\x00','little')
255
>>> int.from_bytes(b'\xFF\x00','big')
65280
like image 85
Kabie Avatar answered Oct 02 '22 15:10

Kabie