Assuming you’re on at least 3.2, there’s a built in for this:
int.from_bytes(bytes,byteorder, *,signed=False)…
The argument
bytesmust either be a bytes-like object or an iterable producing bytes.The
byteorderargument determines the byte order used to represent the integer. Ifbyteorderis"big", the most significant byte is at the beginning of the byte array. Ifbyteorderis"little", the most significant byte is at the end of the byte array. To request the native byte order of the host system, usesys.byteorderas the byte order value.The
signedargument indicates whether two’s complement is used to represent the integer.
## Examples: int.from_bytes(b'\x00\x01', "big") # 1 int.from_bytes(b'\x00\x01', "little") # 256 int.from_bytes(b'\x00\x10', byteorder='little') # 4096 int.from_bytes(b'\xfc\x00', byteorder='big', signed=True) #-1024