J_yang
J_yang

Reputation: 2812

How to interpret python bytes class when more than one byte in one \x

In Python, I am struggling to understand the following bytes:

a = b'\xff33'
b = b'\x00333'
c = b'\x00gff'

According to Pycharm debugger, a has 3 bytes, b c have 4 bytes. I don't quite get why.

Why b'\xff33' != b'\xff\x33' and b'\x00333' != b'\x00\x03\x33', and same logic for c

And a b c 's hex() conversion show:

a.hex() == 'ff3333'
b.hex() == '00333333'
c.hex() == '00676666'

I can't make sense of the results. Especially for c.hex(). It seems g == 67 and f == 66... But then why a.hex() is ff not 6666.. I feel that my head is exploding.

Can you help me make sense of these?

Thanks

J

Upvotes: 0

Views: 170

Answers (1)

Wups
Wups

Reputation: 2569

Python's bytes uses a mix of escaped unprintable bytes and normal printable bytes. a consists of the unprintable and escaped byte \xff and 2 printable "3".
That would be \xff\x33\x33 if one would escape all bytes, which is the same as the .hex() result.

Upvotes: 1

Related Questions