int.from_bytes()

The inverse of to_bytes, and a classmethod — you call it on the int type, not on a value. Any length of input is accepted, including empty.

Int classmethodPython 3.2+Live demo
Common call
int.from_bytes(blob, 'big')
Returns
int — arbitrarily large if the input is long
Replaces
struct.unpack for simple fixed-width integers
Watch out
called on the TYPE: int.from_bytes(...), not some_int.from_bytes(...)
int.from_bytes(bytesbytes — A bytes, bytearray, or any iterable of ints in range(256). Empty is allowed and yields 0.type: bytes-like · required, byteorderbyteorder — 'big' reads the most significant byte first; 'little' reads it last. Since 3.11 it defaults to 'big'.type: str · default: 'big', *, signedsigned — Keyword-only. True interprets the input as two-complement, so a high top bit means a negative result.type: bool · default: False=False)
→ int

Demo

Live evaluation
Try:
Inputs
valslistbyte values, 0-255, comma separated
byteorderstr'big' or 'little'
Output
int.from_bytes(bytes([0, 255]), 'big')
255

from_bytes reads the bytes as digits of a base-256 number. Byte order decides which end is most significant, which is why b"\x00\xff" is 255 big-endian but 65280 little-endian. Unlike to_bytes there is no width to overflow — any number of bytes is valid, and empty input is defined as 0. Leading zero bytes simply do not contribute.

Parameters

NameTypeRequiredDescription
bytesbytes-likeyesA bytes, bytearray, or any iterable of ints in range(256). Empty is allowed and yields 0.
byteorderstrno ('big')'big' reads the most significant byte first; 'little' reads it last. Since 3.11 it defaults to 'big'.
signedboolno (False)Keyword-only. True interprets the input as two-complement, so a high top bit means a negative result.

Return value

int — The integer the bytes represent. Empty bytes give 0. Never raises for width — any length is valid.

Common patterns

Read a length prefix off a wire
The mirror image of packing the header with to_bytes.
length = int.from_bytes(header[:4], 'big')
Turn a hash digest into a number
Any digest is just bytes; from_bytes gives you the integer behind it.
value = int.from_bytes(hashlib.sha256(data).digest(), 'big')
Signed round trip
Pass signed=True on both halves or the values will not match.
raw = (-1).to_bytes(2, 'big', signed=True)
int.from_bytes(raw, 'big', signed=True)   # -1

Examples

1. Big-endian
int.from_bytes(b'\x00\xff', 'big')
Returns
255
2. Little-endian
int.from_bytes(b'\x00\xff', 'little')
Returns
65280
3. Two full bytes
int.from_bytes(b'\xff\xff', 'big')
Returns
65535
4. Empty is zero
int.from_bytes(b'', 'big')
Returns
0
5. Signed reads negative
int.from_bytes(b'\xff\xff', 'big', signed=True)
Returns
-1
6. Any length works
int.from_bytes(b'\x01' * 32, 'big')
Returns
a very large int

Pitfalls

1. Mismatched byteorder silently gives a wrong number
This is the big one. Packing big-endian and reading little-endian does not raise — it hands back a plausible but wrong value. 255 becomes 65280 and nothing complains.
Silent corruption
raw = (255).to_bytes(2, 'big')
int.from_bytes(raw, 'little')
65280
Same order both ways
raw = (255).to_bytes(2, 'big')
int.from_bytes(raw, 'big')
255
2. Forgetting signed=True on the way back
Two-complement bytes read as unsigned produce a large positive number instead of the negative you stored. Like byteorder, the flag has to match on both sides.
Lost the sign
raw = (-1).to_bytes(2, 'big', signed=True)
int.from_bytes(raw, 'big')
65535
Signed both ways
int.from_bytes(raw, 'big', signed=True)
-1
3. It is a classmethod
It lives on the type. Calling it on an instance works, because the instance finds the same classmethod, but it reads as though the value matters when it does not.
Misleading
(0).from_bytes(b'\x01', 'big')
1 # the 0 was ignored entirely
Call on the type
int.from_bytes(b'\x01', 'big')
1

When to use

Use it
  • Reading fixed-width integers out of a binary protocol or file
  • Converting a digest or nonce into a number
  • Round-tripping values written with to_bytes
  • Decoding arbitrary-length big integers, which struct cannot do
Reach for something else
  • Unpacking several fields at once → struct.unpack
  • Parsing text digits → int(s) or int(s, base)
  • Untrusted length prefixes → validate before allocating on the result

Notes

Complexity
O(len(bytes)) — one pass over the input
Return
A new int, arbitrarily large for long inputs
CPython impl
Objects/longobject.c :: int_from_bytes_impl
Memory
Allocates an int wide enough for the input
Thread-safe
Yes — ints and bytes are immutable

FAQ

It falls out of reading the input as a base-256 number: an empty sequence of digits sums to nothing, so the value is 0. It also makes from_bytes total — no length is invalid — which is why only to_bytes can raise OverflowError.

int.from_bytes(b'', 'big')
# 0

History

3.2
int.from_bytes added alongside int.to_bytes.
3.11
byteorder defaults to "big".