Python 3.2+Numbers & mathfunction
int.from_bytes
bytes
Class method: reconstruct an integer from a bytes object using the given byte order.
Syntax
int.from_bytes(bytes, byteorder='big', *, signed=False)
The byte order is not stored anywhere, you must know how the data was written: the same two bytes decode to 7 with 'big' and to 1792 with 'little', and a wrong guess raises nothing. With the default signed=False a leading 0xFF reads as a large positive number instead of -1, so pass signed=True whenever the producer used a signed encoding. byteorder only became optional (defaulting to 'big') in Python 3.11.
Examples
print(int.from_bytes(b'\x00\x07', 'big')) # → 7
print(int.from_bytes(b'\xff', 'little')) # → 255
print(int.from_bytes(b'\x01\x00', 'little')) # → 1
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy