Peter
Peter

Reputation: 467

Convert bytes array to int python wrong result

I know this should be easy, but I just can't get the syntax right for python.

My int is not converted correctly. This is the output of my 2 print statements. My output should be 9718 instead of 959918392.

bytearray(b'9718')
959918392

This is my conversion. I don't understand what am I doing wrong.

print(size)
print(int.from_bytes(size, byteorder='big'))

Upvotes: 1

Views: 667

Answers (1)

Lionel Foxcroft
Lionel Foxcroft

Reputation: 1747

What you tried assumes the number is directly encoded as bytes. You actually want to parse it from ascii, which you can do like this:

int(b'9718'.decode('ascii'))

Upvotes: 1

Related Questions