How to Convert Bytes to Int in Python?

In this tutorial, we will explore different methods to convert bytes to integers in Python. Converting bytes to integers is a common task when dealing with binary data, such as reading data from files or network sockets. By converting bytes to integers, we can perform various arithmetic and logical operations, interpret data, and manipulate it as needed.

Using int.from_bytes() Method

The int.from_bytes() method is the standard way to create an integer from a sequence of bytes. It takes two main parameters: the bytes to convert and the byte order ('big' or 'little').

Basic Conversion

Let's convert a sequence of bytes representing the number 170 in little-endian byte order ?

data = b'\xAA\x00\x00\x00'
integer = int.from_bytes(data, byteorder='little')
print(f"Bytes: {data}")
print(f"Integer: {integer}")
Bytes: b'\xaa\x00\x00\x00'
Integer: 170

Big-Endian vs Little-Endian

The byte order determines how multi-byte numbers are stored. Here's a comparison ?

data = b'\x01\x02\x03\x04'

# Big-endian: most significant byte first
big_endian = int.from_bytes(data, byteorder='big')
print(f"Big-endian: {big_endian}")

# Little-endian: least significant byte first  
little_endian = int.from_bytes(data, byteorder='little')
print(f"Little-endian: {little_endian}")
Big-endian: 16909060
Little-endian: 67305985

Signed vs Unsigned Integers

The signed parameter controls whether to interpret bytes as signed or unsigned integers ?

data = b'\xFF\xFF\xFF\xFF'

# Unsigned interpretation (default)
unsigned = int.from_bytes(data, byteorder='big', signed=False)
print(f"Unsigned: {unsigned}")

# Signed interpretation
signed = int.from_bytes(data, byteorder='big', signed=True)
print(f"Signed: {signed}")
Unsigned: 4294967295
Signed: -1

Alternative Methods

Using struct Module

The struct module provides another way to convert bytes to integers ?

import struct

data = b'\x01\x02\x03\x04'

# Unpack as big-endian unsigned int
result = struct.unpack('>I', data)[0]
print(f"Using struct: {result}")

# Compare with int.from_bytes
result2 = int.from_bytes(data, byteorder='big')
print(f"Using int.from_bytes: {result2}")
Using struct: 16909060
Using int.from_bytes: 16909060

Comparison

Method Pros Cons Best For
int.from_bytes() Simple, readable, built-in Python 3.2+ only Most use cases
struct.unpack() More format options More complex syntax Complex binary formats

Common Use Cases

Here's a practical example of reading binary data from a file ?

# Simulate reading 4-byte integers from binary data
binary_data = b'\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x03'

integers = []
for i in range(0, len(binary_data), 4):
    chunk = binary_data[i:i+4]
    integer = int.from_bytes(chunk, byteorder='big')
    integers.append(integer)

print(f"Converted integers: {integers}")
Converted integers: [1, 2, 3]

Conclusion

The int.from_bytes() method is the most straightforward way to convert bytes to integers in Python. Remember to specify the correct byte order and use the signed parameter when dealing with signed integers. For more complex binary formats, consider using the struct module.

Updated on: 2026-03-27T09:16:56+05:30

32K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements