Assuming you're on at least 3.2, there's a built in for this:

int.from_bytes( bytes, byteorder, *, signed=False )

...

The argument bytes must either be a bytes-like object or an iterable producing bytes.

The byteorder argument determines the byte order used to represent the integer. If byteorder is "big", the most significant byte is at the beginning of the byte array. If byteorder is "little", the most significant byte is at the end of the byte array. To request the native byte order of the host system, use sys.byteorder as the byte order value.

The signed argument indicates whether two’s complement is used to represent the integer.

## Examples:
int.from_bytes(b'\x00\x01', "big")                         # 1
int.from_bytes(b'\x00\x01', "little")                      # 256

int.from_bytes(b'\x00\x10', byteorder='little')            # 4096
int.from_bytes(b'\xfc\x00', byteorder='big', signed=True)  #-1024
Answer from Peter DeGlopper on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-convert-bytes-to-int-in-python
How to Convert Bytes to Int in Python? - GeeksforGeeks
July 23, 2025 - The struct.unpack() function is part of Python's struct module. It unpacks a byte object into a tuple of values according to the format specified.
Discussions

python - Why does "bytes(n)" create a length n byte string instead of converting n to a binary representation? - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... Save this question. Show activity on this post. I was trying to build this bytes object in Python 3: More on stackoverflow.com
🌐 stackoverflow.com
python - How to convert a string of bytes into an int? - Stack Overflow
How can I convert a string of bytes into an int in python? Say like this: 'y\xcc\xa6\xbb' I came up with a clever/stupid way of doing it: sum(ord(c) More on stackoverflow.com
🌐 stackoverflow.com
Byte to integer ? why so complicated in python?
Hi, does anyone know how to read a byte from a binary file and convert to an integer I been busy for one hour on this, while it should be so simple, I keep running into errors after errors f = open(filename, "rb") a =… More on blenderartists.org
🌐 blenderartists.org
2
0
July 19, 2022
Converting integer to byte string problem in python 3
I have a library function that is failing because I cannot seem to properly convert an integer to a proper byte string except by using a literal. Can someone explain why I get 3 different results from 3 different ways to convert an integer to a byte string? Using a literal, which is the only ... More on discuss.python.org
🌐 discuss.python.org
6
0
August 27, 2020
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-convert-bytes-to-int-in-python
How to Convert Bytes to Int in Python?
March 27, 2026 - The int.from_bytes() method is the standard way to create an integer from a sequence of bytes.
🌐
Python
docs.python.org › 3 › builtins › stdtypes.html
Built-in Types — Python 3.14.7 documentation
If byteorder is "little", the most significant byte is at the end of the byte array. The signed argument determines whether two’s complement is used to represent the integer. If signed is False and a negative integer is given, an OverflowError is raised.
🌐
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.
🌐
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.
Find elsewhere
🌐
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.from_bytes() method returns the integer represented by the given byte array. The first argument the method takes must be a bytes-like object or an iterable that produces bytes.
🌐
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 - #python · 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 ·
🌐
Appdividend
appdividend.com › convert-int-to-bytes-and-bytes-to-int-in-python
How to Convert Int to Bytes and Bytes to Int in Python
September 14, 2025 - You can convert back bytes to an integer optimally using the int.from_bytes() method. It accepts bytes that we want to convert back to an integer, byteorder, which can be big or little, and signed (optional argument), which is either True or False.
🌐
Blender Artists
blenderartists.org › technical support › python support
Byte to integer ? why so complicated in python? - Python Support - Blender Artists Community
July 19, 2022 - Hi, does anyone know how to read a byte from a binary file and convert to an integer I been busy for one hour on this, while it should be so simple, I keep running into errors after errors f = open(filename, "rb") a = bytearray(b'\x00') a.append(f.read(1))
🌐
Python.org
discuss.python.org › python help
Converting integer to byte string problem in python 3 - Python Help - Discussions on Python.org
August 27, 2020 - I have a library function that is failing because I cannot seem to properly convert an integer to a proper byte string except by using a literal. Can someone explain why I get 3 different results from 3 different ways to convert an integer to a byte string? Using a literal, which is the only way the library call works produces this: >>> print(b'1') b'1' Using the builtin bytes function, which the library call rejects, is really strange: >>> i=1 >>> print(bytes(i)) b'\x00' Finally using the...
🌐
DEV Community
dev.to › kiani0x01 › python-bytes-to-int-conversion-42b9
Python Bytes to Int Conversion - DEV Community
July 27, 2025 - 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. Python provides a built-in method, but it helps to know what’s happening under the hood.
🌐
Finxter
blog.finxter.com › home › learn python blog › 5 best ways to convert python bytes to int – big endian
5 Best Ways to Convert Python Bytes to Int - Big Endian - Be on the Right Side of Change
February 23, 2024 - This article will explore different ... should result in the integer 16777216. The Python built-in int.from_bytes() function can convert a bytes object to an integer....
🌐
GitHub
gist.github.com › d81604135a1b94b773b6
How to convert bytes to integer in python · GitHub
How to convert bytes to integer in python. GitHub Gist: instantly share code, notes, and snippets.
🌐
Reddit
reddit.com › r/learnpython › convert a string byte to integer
r/learnpython on Reddit: Convert a string byte to integer
September 14, 2020 -

Hi, I have a peculiar setup. I have a byte array converted to a string. It resembles something like: bytearray(b'\xfa\xff\xff\xff\xff\xff\xff....

The string arrives from a TCP socket, and I want to convert this string into a list or numpy array of integers from 0 to 255.

I am able to convert to a list resembling this: 'ff', 'ff', 'f9', 'ff', 'ff', 'ff', '00', '00', '00', '00', 'f6', 'ff', 'ff', 'ff', '00', '00', '00',...

However, using decode('utf-8') did not seem to work.

I have tried several methods with limited outcomes. For example, for loops and lambdas to iterate through each loop, trying to use int(), etc.

Since this is python 3, I get errors stating that 'Str' and list have no attribute 'decode.'

Any suggestions on making this more efficient? Some Codec function perhaps?

🌐
GeeksforGeeks
geeksforgeeks.org › how-to-convert-int-to-bytes-in-python
How to Convert Int to Bytes in Python? - GeeksforGeeks
February 24, 2025 - For example, if the input is b'hello', the output will be 'hello'.This article covers different ways to convert bytes into strings in Pyt ... In Python, you can convert a float to an integer using type conversion.
🌐
CodeRivers
coderivers.org › blog › python-bytes-to-int
Python Bytes to Int: A Comprehensive Guide - CodeRivers
February 22, 2026 - In this example, the bytes object b contains two bytes 0x00 and 0x10. Using byteorder='big', the resulting integer is 16 (since 0x10 in decimal is 16). The struct module in Python can also be used to convert bytes to int. This is useful when dealing with more complex binary data structures.
🌐
w3resource
w3resource.com › python-exercises › python-basic-exercise-94.php
Python: Convert a byte string to a list of integers - w3resource
May 17, 2025 - # Define a string named S. S = "The quick brown fox jumps over the lazy dog." # Print a message indicating the original string. print("Original string:") # Print the original string. print(S) # Create an empty list named nums. nums = [] # Print a message to indicate the conversion of bytes to a list of integers.