5

I am using python 3 and try to convert a hex-string to a byte-represented form. So i used the following command:

bytes.fromhex('97ad300414b64c')

I Expected results like this: b'\x97\xad\x30\x04\x14\xb6\x4c'' but got b'\x97\xad0\x04\x14\xb6L'. I am note sure what i am doing wrong, but maybe it is something with the encoding?

5
  • 4
    Everything is fine. The print function tries to make things 'look' friendly. And doesn't in this case. print(bytes.fromhex('4d65727279204368726973746d6173')) Commented Dec 27, 2017 at 18:15
  • print(repr(b"\x30")) Commented Dec 27, 2017 at 18:15
  • 4
    Some of the bytes correspond to printable characters. \x30 => '0' and \x4c => 'L'. Commented Dec 27, 2017 at 18:24
  • (both are confusion about the representation of bytes in Python 3 after converting from hex string.) Commented Feb 12, 2021 at 12:44
  • as you said: my main problem was the interpretation of the output representation. The answer in your suggestion clears this up. (Using binascii.hexlify) is what i needed at this time. Commented Feb 12, 2021 at 15:13

1 Answer 1

0

As pointed by @user8651755 in the comments, this is due to the fact that some bytes correspond to printable characters. So the answer is: you are doing everything right.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.