I have an array of integers (all less than 255) that correspond to byte values, e.g. [55, 33, 22]
. How can I turn that into a bytes object that would look like b'\x55\x33\x22'
?
3 Answers
Just call the bytes
constructor.
As the docs say:
… constructor arguments are interpreted as for
bytearray()
.
And if you follow that link:
If it is an iterable, it must be an iterable of integers in the range
0 <= x < 256
, which are used as the initial contents of the array.
So:
>>> list_of_values = [55, 33, 22]
>>> bytes_of_values = bytes(list_of_values)
>>> bytes_of_values
b'7!\x16'
>>> bytes_of_values == b'\x37\x21\x16'
True
Of course the values aren't going to be \x55\x33\x22
, because \x
means hexadecimal, and the decimal values 55, 33, 22
are the hexadecimal values 37, 21, 16
. But if you had the hexadecimal values 55, 33, 22
, you'd get exactly the output you want:
>>> list_of_values = [0x55, 0x33, 0x22]
>>> bytes_of_values = bytes(list_of_values)
>>> bytes_of_values == b'\x55\x33\x22'
True
>>> bytes_of_values
b'U3"'
-
nice but could you please explain why "b'7!\x16'" is equal to '\x37\x21\x16'? – totalMongot Jan 07 '22 at 20:30
-
2@totalMongot the ASCII value for 0x37 is "7" (digit 7) and the ASCII value for 0x21 is "!" (exclamation point). The ASCII value for 0x16 is SYN (control character synchronous idle). Since SYN is a non-printing character, it is represented in a bytes object by the hex escape \x16 – James Duvall Sep 22 '22 at 18:47
-
@totalMongot There was a typo where that was written as a string (`'\x37\x21\x16'`) instead of bytes (`b'\x37\x21\x16'`), if that's what confused you. I fixed it. – wjandrea Jan 02 '23 at 16:25
The bytes
constructor takes an iterable of integers, so just feed your list to that:
l = list(range(0, 256, 23))
print(l)
b = bytes(l)
print(b)
Output:
[0, 23, 46, 69, 92, 115, 138, 161, 184, 207, 230, 253]
b'\x00\x17.E\\s\x8a\xa1\xb8\xcf\xe6\xfd'
See also: Python 3 - on converting from ints to 'bytes' and then concatenating them (for serial transmission)

- 3,856
- 4
- 21
- 43
-
And in Python 2, `bytes(bytearray(l))`, since `bytes is str` in Python 2. – user2357112 Jun 05 '15 at 07:33
struct.pack("b"*len(my_list), *my_list)
I think will work
>>> my_list = [55, 33, 22]
>>> struct.pack("b"*len(my_list), *my_list)
b'7!\x16'
If you want hex, you need to make it hex in the list
>>> my_list = [0x55, 0x33, 0x22]
>>> struct.pack("b"*len(my_list), *my_list)
b'U3"'
In all cases, if that value has an ASCII representation, it will display it when you try to print it or look at it.

- 28,235
- 9
- 60
- 81

- 110,522
- 12
- 160
- 179
-
this is close I think, i do get this error `struct.error: byte format requires -128 <= number <= 127` Can you help with that? – Startec Jun 05 '15 at 05:59
-
4@Startec The `'b'` format means signed bytes [-128, 127]. You want to use a capital `'B'` for _un_signed bytes [0,255]. – Kevin J. Chase Jun 05 '15 at 07:31
-
3
-
heh yeah but you could :P ... (Im not in py3 yet I now see there are much better solutions :P , that also apply to py2 I think) – Joran Beasley Jun 05 '15 at 15:23
-
FWIW, you don't need to do string multiplication, just put the number. `struct.pack(f"{len(my_list)}b", *my_list)` – wjandrea Jan 02 '23 at 16:32