Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Convert a byte array back to list of int

Tags:

python

arrays

I can convert a list of ints into a byte array with the following:

bytes([17, 24, 121, 1, 12, 222, 34, 76])
Out[144]: b'\x11\x18y\x01\x0c\xde"L'
bytes([1, 2, 3])
Out[145]: b'\x01\x02\x03'

What I want now is to get the byte array string back to its original list. Is there an easy python function to do that? I found the following:

int.from_bytes(b'\x11\x18y\x01\x0c\xde"L', byteorder='big', signed=False)
Out[146]: 1231867543503643212

I am not quite sure what is happening here. How is the conversion happening and what does the output signify. So if anyone can provide some context or insight, I will be grateful

like image 897
Fizi Avatar asked Nov 06 '25 05:11

Fizi


1 Answers

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]
like image 133
Stephen Rauch Avatar answered Nov 08 '25 00:11

Stephen Rauch