Of course! The int.from_bytes() method is a built-in Python function for converting a bytes object (or a bytearray) into an integer.

This is an incredibly useful function, especially when dealing with low-level data, network protocols, file formats, or cryptography where data is often represented as raw bytes.
The Basics: Syntax and Parameters
The method is called on the int class and has the following signature:
class int.from_bytes(bytes, byteorder, *, signed=False)
Let's break down the parameters:
-
bytes: This is the required input. It can be any object that supports the buffer protocol, most commonly:
(图片来源网络,侵删)- A
bytesobject (e.g.,b'\x01\x02') - A
bytearrayobject (e.g.,bytearray([1, 2]))
- A
-
byteorder: This string specifies the order of the bytes. It's one of the most critical parameters.'big': Big-endian. The most significant byte (the one with the highest value) is at the beginning of the byte sequence (the smallest memory address). This is the network standard (network byte order).'little': Little-endian. The least significant byte is at the beginning. This is common for x86 and x64 processors (Intel, AMD).'native': Uses the native byte order of the system your Python code is running on. This can be useful for creating portable applications, but be explicit ('big'or'little') whenever possible for clarity and safety.
-
signed: This is a boolean keyword-only argument (you must usesigned=Trueorsigned=False).False(default): The integer is treated as an unsigned value (it cannot be negative).True: The integer is treated as a signed two's complement value. This means the most significant bit of the byte sequence is interpreted as the sign bit.
How It Works: The Core Concept
An integer is just a sequence of bits. int.from_bytes() interprets that sequence of bits in a specific order (big or little endian) and optionally applies the two's complement rule to determine the sign.
- For unsigned numbers: It's a direct mapping. The value is the sum of each byte's value multiplied by
256^position. - For signed numbers: It first checks the most significant bit of the entire byte sequence.
- If it's
0, the number is positive, and the value is calculated the same way as an unsigned number. - If it's
1, the number is negative. To find its value, the function calculates the "unsigned" value and then applies the two's complement operation to get the negative integer.
- If it's
Practical Examples
Let's use the 4-byte sequence b'\x01\x02\x03\x04' for most examples. In binary, this is 00000001 00000010 00000011 00000100.

Example 1: Unsigned Integers
data = b'\x01\x02\x03\x04'
# Big-endian: 01 02 03 04
# Value = (1 * 256^3) + (2 * 256^2) + (3 * 256^1) + (4 * 256^0)
# Value = 16777216 + 131072 + 768 + 4 = 16909060
big_endian_int = int.from_bytes(data, 'big')
print(f"Big-endian unsigned: {big_endian_int}") # Output: 16909060
# Little-endian: 04 03 02 01
# Value = (4 * 256^3) + (3 * 256^2) + (2 * 256^1) + (1 * 256^0)
# Value = 67372036 + 50388 + 512 + 1 = 67422937
little_endian_int = int.from_bytes(data, 'little')
print(f"Little-endian unsigned: {little_endian_int}") # Output: 67422937
Example 2: Signed Integers
This is where it gets interesting. Let's use a byte sequence where the most significant bit is 1, which indicates a negative number in two's complement.
# The byte sequence b'\xff\xff\xff\xfe' represents -2 in a 4-byte signed integer.
# In binary: 11111111 11111111 11111111 11111110
data = b'\xff\xff\xff\xfe'
# Big-endian signed
# The first bit of the first byte is 1, so it's negative.
# The unsigned value is 4294967294.
# Two's complement: -(unsigned_value + 1) = -(4294967294 + 1) = -4294967295
# Wait, let's double-check the Python calculation.
# Python's integers are arbitrary precision, so the result is just -2.
big_endian_signed = int.from_bytes(data, 'big', signed=True)
print(f"Big-endian signed: {big_endian_signed}") # Output: -2
# Little-endian signed
# The last byte is '\xfe', its first bit is 1, so it's negative.
little_endian_signed = int.from_bytes(data, 'little', signed=True)
print(f"Little-endian signed: {little_endian_signed}") # Output: -2
Example 3: Handling Different Lengths
The number of bytes directly determines the range of the integer.
# 1 byte (8 bits)
# Unsigned range: 0 to 255
# Signed range: -128 to 127
one_byte_unsigned = int.from_bytes(b'\xff', 'big', signed=False)
one_byte_signed = int.from_bytes(b'\xff', 'big', signed=True)
print(f"1-byte unsigned 0xff: {one_byte_unsigned}") # Output: 255
print(f"1-byte signed 0xff: {one_byte_signed}") # Output: -1
# 2 bytes (16 bits)
# Unsigned range: 0 to 65535
# Signed range: -32768 to 32767
two_bytes_unsigned = int.from_bytes(b'\x7f\xff', 'big', signed=False)
two_bytes_signed = int.from_bytes(b'\x7f\xff', 'big', signed=True)
print(f"2-byte unsigned 0x7fff: {two_bytes_unsigned}") # Output: 32767
print(f"2-byte signed 0x7fff: {two_bytes_signed}") # Output: 32767
# Let's try a negative 16-bit number
two_bytes_negative = int.from_bytes(b'\xff\x7f', 'big', signed=True)
print(f"2-byte signed 0xff7f: {two_bytes_negative}") # Output: -32769
The Opposite Operation: int.to_bytes()
Just as you can convert bytes to an int, you can convert an int back to bytes. This is the inverse operation and uses the same parameters (byteorder and signed).
my_number = 16909060
# Convert the integer back to bytes
# We must specify the length of the byte sequence.
# For 16909060, 4 bytes are enough.
bytes_big_endian = my_number.to_bytes(4, 'big')
bytes_little_endian = my_number.to_bytes(4, 'little')
print(f"Integer 16909060 -> Big-endian bytes: {bytes_big_endian}")
# Output: Integer 16909060 -> Big-endian bytes: b'\x01\x02\x03\x04'
print(f"Integer 16909060 -> Little-endian bytes: {bytes_little_endian}")
# Output: Integer 16909060 -> Little-endian bytes: b'\x04\x03\x02\x01'
# Example with a negative number
negative_number = -2
bytes_negative = negative_number.to_bytes(4, 'big', signed=True)
print(f"Integer -2 -> Big-endian signed bytes: {bytes_negative}")
# Output: Integer -2 -> Big-endian signed bytes: b'\xff\xff\xff\xfe'
Common Use Cases
-
Network Programming: Many network protocols (like TCP/IP) specify that multi-byte numbers (like port numbers or IP addresses) should be in big-endian format. You'll use
int.from_bytes()constantly when parsing network packets. -
Binary File Formats: Reading files like images (PNG, BMP), executables, or database files often involves interpreting fixed-size chunks of data as integers.
-
Cryptography and Hashing: Cryptographic hashes (like SHA-256) are represented as a sequence of bytes.
int.from_bytes()is essential to convert this hash into a large integer for use in algorithms like Digital Signature Algorithm (DSA). -
Hardware Interaction: When communicating with microcontrollers or other hardware devices, data is often sent as raw byte streams that need to be interpreted as integers.
