This is kind of an old thread, but in Python 3.2+ now you can simply say:

number = 100
number.to_bytes(4, byteorder = 'big')

or byteorder = 'little' as per your needs. Documentation here.

Answer from charlie80 on Stack Overflow
🌐
Python Forum
python-forum.io › thread-36099.html
How to convert 4 bytes to an integer ?
Hi, I am using SPI on Raspberry Pi (master) and collecting 4 bytes at a time. I need to convert this back to integer. How can I do it? When I do it using struct...it gives some random garbage values: b=spi.readbytes(4) value=struct.un...
Discussions

python - How to convert a string of bytes into an int? - Stack Overflow
Result equals the result of Test 4, as expected. ... Save this answer. ... Show activity on this post. I was struggling to find a solution for arbitrary length byte sequences that would work under Python 2.x. Finally I wrote this one, it's a bit hacky because it performs a string conversion, but it works. Copydef signedbytes(data): """Convert a bytearray into ... More on stackoverflow.com
🌐 stackoverflow.com
python - Convert 4 bytes to integer using struct - Stack Overflow
Explore Stack Internal ... Save this question. Show activity on this post. I have a microcontroller connected to my computer via I2C connection, which sends back 1 byte of data at a time. I am trying to send a 4-byte number to the python program. I receive it in an array of individual bytes like this [123,45,67,89]. I need to convert ... More on stackoverflow.com
🌐 stackoverflow.com
python - Converting 4 bytes to an integer - Stack Overflow
I am learning Python and would like to convert some data from a binary header file to integer values. I know the structure of the header file (already read in with C++) and I know which bytes form an integer. I have read through some of the answers and all of them point to the class struct and the method unpack. However I don't know how to feed in the 4 ... More on stackoverflow.com
🌐 stackoverflow.com
How is a integer mapped to 4 bytes? How to convert 4 bytes into integer in python - Stack Overflow
In this case, it looks like integer 900 is somehow mapped into [0,0,3,132]. What is going on here? how are they converting 900 to 4 bytes? How do I convert [0,0,3,132] and [0,0,4,176] back to 900 and 1200 in python?? More on stackoverflow.com
🌐 stackoverflow.com
People also ask

How can I convert bytes to an integer in Python?
You can use several methods to convert bytes to an integer in Python. Some commonly used methods include using the int.from_bytes() function, unpacking with the struct module, and using the ord() function.

Each method has its own advantages and use cases so that you can choose the most suitable one based on your programming requirements.
🌐
codingdeeply.com
codingdeeply.com › home › easily convert bytes to int in python: step-by-step guide
Easily Convert Bytes to Int in Python: Step-by-Step Guide
How can I use the int.from_bytes() function to convert bytes to an integer?
To use the int.from_bytes() function, you must pass the bytes object and the byte order as parameters. For example, if you have a bytes object named 'byte_data' and want to convert it to an integer using big-endian byte order, you can use the following syntax: 'integer = int.from_bytes(byte_data, byteorder="big")'.

This will convert the bytes object to an integer and assign it to the 'integer' variable.
🌐
codingdeeply.com
codingdeeply.com › home › easily convert bytes to int in python: step-by-step guide
Easily Convert Bytes to Int in Python: Step-by-Step Guide
How can I convert bytes to an integer using unpacking with the struct module?
To convert bytes to an integer using unpacking with the struct module, you must define a format string specifying the byte order and size.

The format string can be created using the '>' or '<' character to indicate big-endian or little-endian byte order, followed by a letter representing the desired byte size (e.g., 'i' for 4-byte integer).

Once you have the format string, you can use the struct.unpack() function to unpack the bytes object and convert it to an integer.
🌐
codingdeeply.com
codingdeeply.com › home › easily convert bytes to int in python: step-by-step guide
Easily Convert Bytes to Int in Python: Step-by-Step Guide
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-convert-bytes-to-int-in-python
How to Convert Bytes to Int in Python? - GeeksforGeeks
July 23, 2025 - Signed integer (b'\xff\xff\xff\xfc'): The format string '>i' unpacks the 4 bytes as a signed integer. The value is interpreted as -4 in two's complement form. If you're working with large amounts of binary data, numpy provides a convenient method ...
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-convert-bytes-to-int-in-python
How to Convert Bytes to Int in Python?
March 27, 2026 - # Simulate reading 4-byte integers ... integers.append(integer) print(f"Converted integers: {integers}") ... The int.from_bytes() method is the most straightforward way to convert bytes to integers in Python....
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 53582537 › converting-4-bytes-to-an-integer
python - Converting 4 bytes to an integer - Stack Overflow
The bytes are now in a list. mybytes = (byte[0],byte[1],byte[2],byte[3]) Is it possible to pass this list to unpack and get the integer? None of the answers so far worked for me. I tried the following: ... which then throws an error of the mybytes being not a string of 4 characters which is fair enough because it isn't string.
🌐
Codingdeeply
codingdeeply.com › home › easily convert bytes to int in python: step-by-step guide
Easily Convert Bytes to Int in Python: Step-by-Step Guide
February 23, 2024 - For example, if you convert to ... raised. Another method for converting bytes to int in Python is by using the struct module to unpack the binary data....
🌐
Delft Stack
delftstack.com › home › howto › python › how to convert bytes to integers
How to Convert Bytes to Int in Python 2.7 and 3.x | Delft Stack
February 2, 2024 - struct.unpack method in Python 2.7/Python 3.x and int.from_bytes() method in Python 3.x could convert bytes to integers.
🌐
Milddev
milddev.com › python-bytes-to-int-conversion
Python Bytes to Int Conversion
July 27, 2025 - A bytes object in Python is an immutable sequence of integers in the range 0–255. It’s like a tiny array: ... Each element maps to one byte. When you need a single integer from this sequence—say, to decode a 4-byte header—you transform those bytes into a single int value.
🌐
Finxter
blog.finxter.com › home › learn python blog › 5 best ways to convert python bytes to int
5 Best Ways to Convert Python Bytes to Int - Be on the Right Side of Change
February 23, 2024 - The challenge is to convert this byte data, possibly b'\x00\x10', to its integer equivalent, 16. Python’s built-in method int.from_bytes() allows for straightforward conversion of bytes to an integer.
🌐
Bobby Hadz
bobbyhadz.com › blog › convert-int-to-bytes-in-python
How to convert Int to Bytes and Bytes to Int in Python | bobbyhadz
The int_to_bytes function takes an integer as a parameter and converts it to a bytes object.
🌐
Coderwall
coderwall.com › p › x6xtxq › convert-bytes-to-int-or-int-to-bytes-in-python
Convert bytes to int or int to bytes in python (Example)
September 29, 2021 - def bytes_to_int(bytes): result = 0 for b in bytes: result = result * 256 + int(b) return result def int_to_bytes(value, length): result = [] for i in range(0, length): result.append(value >> (i * 8) & 0xff) result.reverse() return result · #python · Say Thanks · Respond · 348.5K · 14 · 308.2K · 24 · 256.4K ·
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-convert-int-to-bytes-in-python
How to Convert Int to Bytes in Python? - GeeksforGeeks
July 23, 2025 - The task of converting an integer to bytes in Python involves representing a numerical value in its binary form for storage, transmission, or processing.
🌐
DEV Community
dev.to › kiani0x01 › python-bytes-to-int-conversion-42b9
Python Bytes to Int Conversion - DEV Community
July 27, 2025 - A bytes object in Python is an immutable sequence of integers in the range 0–255. It’s like a tiny array: ... Each element maps to one byte. When you need a single integer from this sequence—say, to decode a 4-byte header—you transform those bytes into a single int value.
🌐
Tutorial Reference
tutorialreference.com › python › examples › faq › python-how-to-convert-bytes-to-int
How to Convert Bytes to Int in Python | Tutorial Reference
The built-in int.from_bytes() method is the most Pythonic, readable, and flexible way to convert bytes to integers.