Bytes representation in Python

Question:

I need to do some work with bytes in Python and I’ve come across a byte string I don’t really understand:

b"Hx00x84xffQx00xa6xff+x00x96xffxc2xffIxffxa5xff'xffx8axffx19xffx19xffxf6xfexb0xfexc7xfeJxfelxfexf8xfd+xfexefxfd:xfexc3xfd*xfe_xfdxdfxfdnxfdxa3xfdxc6xfcqxfdxbdxfc?xfd"

So according to what I know, bytes should be represented as xhh, where hh are hexadecimal values (from 0 to f). However, in the third segment, there is xffQ, and farther on there are other characters which shouldn’t appear: I, ‘, *, :, ? etc.
I’ve used hex() method to see what would be the outcome, and I got this:

480084ff5100a6ff2b0096ffc2ff49ffa5ff27ff8aff19ff19fff6feb0fec7fe4afe6cfef8fd2bfeeffd3afec3fd2afe5ffddffd0afda3fdc6fc71fdbdfc3ffd

As you can see, some parts of the hex are the same, but e.g. xffQ was changed into ff51. I need to append some data to this byte string, so I’d like to know what’s going on there (or how to get the same result).

Asked By: Kate

||

Answers:

Both repr and str when processing a bytes object will print ASCII characters where possible. Otherwise their hexadecimal values will be shown in the form xNN.

It might help you to visualise the content if you print it as all hexadecimal as follows:

b = b"Hx00x84xffQx00xa6xff+x00x96xffxc2xffIxffxa5xff'xffx8axffx19xffx19xffxf6xfexb0xfexc7xfeJxfelxfexf8xfd+xfexefxfd:xfexc3xfd*xfe_xfdxdfxfdnxfdxa3xfdxc6xfcqxfdxbdxfc?xfd"
print(''.join(hex(b_) for b_ in b))

Output:

0x480x00x840xff0x510x00xa60xff0x2b0x00x960xff0xc20xff0x490xff0xa50xff0x270xff0x8a0xff0x190xff0x190xff0xf60xfe0xb00xfe0xc70xfe0x4a0xfe0x6c0xfe0xf80xfd0x2b0xfe0xef0xfd0x3a0xfe0xc30xfd0x2a0xfe0x5f0xfd0xdf0xfd0xa0xfd0xa30xfd0xc60xfc0x710xfd0xbd0xfc0x3f0xfd
Answered By: Stuart

Or you can use binascii module if you want to visualize the content:

import binascii 
print(binascii.hexlify(b"hello world"))

Output:

68656c6c6f20776f726c64
Answered By: funnydman
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.