8

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
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
  • 1
    This isn't a duplicate because the other question is for 16-bit integers, and this is for arbitrary length integers. – amcnabb Feb 01 '13 at 23:24
  • I agree this is not a duplicate, the accepted answer here http://stackoverflow.com/questions/30402743/python-2-7-equivalent-of-built-in-method-int-from-bytes appears to be relevant however – Dan May 20 '16 at 18:14

1 Answers1

18

Since 3.2:

>>> int.from_bytes(b'\xFF\x00','little')
255
>>> int.from_bytes(b'\xFF\x00','big')
65280
Kabie
  • 10,489
  • 1
  • 38
  • 45