Fizi
Fizi

Reputation: 1861

Python: Convert a byte array back to list of int

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

Upvotes: 4

Views: 14292

Answers (1)

Stephen Rauch
Stephen Rauch

Reputation: 49842

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]

Upvotes: 10

Related Questions