You can convert the bytearray to a list of ints with list()

Test Code:

x = bytes([17, 24, 121, 1, 12, 222, 34, 76])
print(x)
print(list(x))

Results:

b'\x11\x18y\x01\x0c\xde"L'
[17, 24, 121, 1, 12, 222, 34, 76]
Answer from Stephen Rauch on Stack Overflow
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-program-to-convert-a-byte-string-to-a-list-of-integers
Python program to convert a byte string to a list of integers - GeeksforGeeks
July 23, 2025 - Byte string is already a sequence of integers (where each byte is an integer between 0 and 255) we can directly convert the byte string to a list of integers using list().
Discussions

Python: Convert a byte array back to list of int - Stack Overflow
You can convert the bytearray to a list of ints with list() More on stackoverflow.com
🌐 stackoverflow.com
python - Convert bytes to int? - Stack Overflow
I'm currently working on an ... convert bytes to an integer. I know that: ... Yet I cannot find out how to do the inverse. What am I doing terribly wrong? ... There is also the struct module if you want to convert multiple variables at once. ... Possible duplicate of Reading integers from binary file in Python, How to convert ... More on stackoverflow.com
🌐 stackoverflow.com
How to convert bytes in a string to integers? Python - Stack Overflow
I want to get a list of ints representing the bytes in a string. More on stackoverflow.com
🌐 stackoverflow.com
Convert a string byte to integer
The standard iterator for bytearray is of type int. ex: resp = bytearray(b'\xfa\xff\xff\xff\xff\xff\xff') for i in resp: print(i) results in... 250 255 255 255 255 255 255 More on reddit.com
🌐 r/learnpython
3
0
September 14, 2020
🌐
Finxter
blog.finxter.com › home › learn python blog › 5 best ways to convert a python list of bytes to int
5 Best Ways to Convert a Python List of Bytes to Int - Be on the Right Side of Change
February 27, 2024 - This method leverages the built-in function int.from_bytes(), which converts a sequence of bytes to an integer. The function requires a bytes-like object and the byte order (‘big’ or ‘little’) as arguments. It’s straightforward and a go-to choice for byte conversion as it’s part of the Python Standard Library. ... This snippet creates a list of bytes, byte_list, and converts it to an integer using int.from_bytes().
🌐
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 called frombuffer() to convert byte data into an array.
🌐
Finxter
blog.finxter.com › home › learn python blog › 5 best ways to convert python bytes to int array
5 Best Ways to Convert Python Bytes to Int Array - Be on the Right Side of Change
February 23, 2024 - The map() function is a powerful built-in function in Python that applies a given function to every item in an iterable. In this method, it helps to efficiently convert each byte to an integer without the need for explicitly writing a loop. ... Instead of iterating over byte_data with a loop, map() takes a function (in this case, int) and applies it to each element of the byte_data, which is then transformed into a list to obtain an array of integers...
🌐
TutorialsPoint
tutorialspoint.com › how-to-convert-bytes-to-int-in-python
How to Convert Bytes to Int in Python?
July 21, 2023 - 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.
🌐
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
🌐
Finxter
blog.finxter.com › home › learn python blog › 5 best ways to convert python bytes to list
5 Best Ways to Convert Python Bytes to List - Be on the Right Side of Change
February 23, 2024 - The built-in list() constructor can convert iterable objects into a list easily. This method is very straightforward and requires only a single function call to transform the bytes object into a list of integers.
🌐
py4u
py4u.org › blog › python-program-to-convert-a-byte-string-to-a-list-of-integers
Python Program to Convert a Byte String to a List of Integers
The most straightforward way to convert a byte string to a list of integers is to leverage Python’s built-in list() constructor.
🌐
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?

🌐
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 ·
Top answer
1 of 2
106

Python doesn't traditionally have much use for "numbers in big-endian C layout" that are too big for C. (If you're dealing with 2-byte, 4-byte, or 8-byte numbers, then struct.unpack is the answer.)

But enough people got sick of there not being one obvious way to do this that Python 3.2 added a method int.from_bytes that does exactly what you want:

int.from_bytes(b, byteorder='big', signed=False)

Unfortunately, if you're using an older version of Python, you don't have this. So, what options do you have? (Besides the obvious one: update to 3.2, or, better, 3.4…)


First, there's your code. I think binascii.hexlify is a better way to spell it than .encode('hex'), because "encode" has always seemed a little weird for a method on byte strings (as opposed to Unicode strings), and it's in fact been banished in Python 3. But otherwise, it seems pretty readable and obvious to me. And it should be pretty fast—yes, it has to create an intermediate string, but it's doing all the looping and arithmetic in C (at least in CPython), which is generally an order of magnitude or two faster than in Python. Unless your bytearray is so big that allocating the string will itself be costly, I wouldn't worry about performance here.

Alternatively, you could do it in a loop. But that's going to be more verbose and, at least in CPython, a lot slower.

You could try to eliminate the explicit loop for an implicit one, but the obvious function to do that is reduce, which is considered un-Pythonic by part of the community—and of course it's going to require calling a function for each byte.

You could unroll the loop or reduce by breaking it into chunks of 8 bytes and looping over struct.unpack_from, or by just doing a big struct.unpack('Q'*len(b)//8 + 'B' * len(b)%8) and looping over that, but that makes it a lot less readable and probably not that much faster.

You could use NumPy… but if you're going bigger than either 64 or maybe 128 bits, it's going to end up converting everything to Python objects anyway.

So, I think your answer is the best option.


Here are some timings comparing it to the most obvious manual conversion:

import binascii
import functools
import numpy as np

def hexint(b):
    return int(binascii.hexlify(b), 16)

def loop1(b):
    def f(x, y): return (x<<8)|y
    return functools.reduce(f, b, 0)

def loop2(b):
    x = 0
    for c in b:
        x <<= 8
        x |= c
    return x

def numpily(b):
    n = np.array(list(b))
    p = 1 << np.arange(len(b)-1, -1, -1, dtype=object)
    return np.sum(n * p)

In [226]: b = bytearray(range(256))

In [227]: %timeit hexint(b)
1000000 loops, best of 3: 1.8 µs per loop

In [228]: %timeit loop1(b)
10000 loops, best of 3: 57.7 µs per loop

In [229]: %timeit loop2(b)
10000 loops, best of 3: 46.4 µs per loop

In [283]: %timeit numpily(b)
10000 loops, best of 3: 88.5 µs per loop

For comparison in Python 3.4:

In [17]: %timeit hexint(b)
1000000 loops, best of 3: 1.69 µs per loop

In [17]: %timeit int.from_bytes(b, byteorder='big', signed=False)
1000000 loops, best of 3: 1.42 µs per loop

So, your method is still pretty fast…

2 of 2
2

Function struct.unpack(...) does what you need.

🌐
w3resource
w3resource.com › python-exercises › extended-data-types › python-extended-data-types-bytes-bytearrays-exercise-3.php
Python Program: Convert list of integers to bytearray
August 11, 2025 - Integer List: [72, 123, 21, 108, 222, 67, 44, 38, 10] Bytearray: bytearray(b'H{\x15l\xdeC,&\n') An error occurred: 'utf-8' codec can't decode byte 0xde in position 4: invalid continuation byte ... Write a Python program to create a bytearray from a list of integers and then modify one of its elements before printing.
🌐
Reddit
reddit.com › r/learnpython › lists to bytes
r/learnpython on Reddit: Lists to Bytes
April 26, 2021 -

I am trying to convert a list into data-type bytes so I can encrypt it. Anyone knows how to do it for a list with strings. Thanks

Here's a example:

list_of_items = ["Cats", "Dogs","Turtle","Humans"]

list_of_items_byte = bytes(list_of_items,"utc-8")

and I get this error:

File "main.py", line 2, in <module>

list_of_items_byte = bytes(list_of_items,"utc-8")

TypeError: encoding without a string argument

🌐
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.
🌐
GitHub
gist.github.com › d81604135a1b94b773b6
How to convert bytes to integer in python · GitHub
How to convert bytes to integer in python · Raw · Byte-to-Integer · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
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.