4

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

0

1 Answer 1

11

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]
Sign up to request clarification or add additional context in comments.

3 Comments

Easier than I imagined
Could you also explain what is happening in int.from_bytes if possible
int.frombytes() builds an integer. It can be big if you pass lots of bytes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.