Why is hexadecimal bytes data b'\x35' being interpreted as b'5' in Python?

Viewed 181

I am working with a set of hexadecimal data. I am using bytes type for the data. For example I am having the data as b'\x35', b'\xA6', b'\x12', b'\x26'

When I print each of these hexadecimal characters, I am getting the output as b'5', b'\xa6', b'\x12', b'&' correspondingly

I am getting b'\xa6', b'\x12' as the same. But I am getting a different output when I print b'\x35' and b'\x26'.

Can you please tell me how do I fix this issue? I want my output to be the same as the input .

1 Answers

The reason you are getting b'5' when you print b'\x35' is because 0x35 is the hexadecimal representation of the number 5 (in the ASCII encoding). Python just interprets b'\x35' as 5. So when you get b'5' its the exact same thing as b'\x35'. The same goes for any ascii character. For example if I typed in:

print(b'\x61')

I would get a as the output because 0x61 is the ascii code for the letter a. Essentially, b'5' is exactly the same as b'\x35', so nothing needs changing. Just use the value as is, since it will work fine.

Hope this helps!

Related