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
\xis an escape sequence, but the other characters just represent the bytes corresponding to their ascii value.a, the the escape sequence\xffrepresents the byte with value 255, i.e. "ff" in hexadecimal. However,3and3each represent a byte, since they aren't escaped with\x, and the value corresponds to the ascii value of the character'3', i.e.ord('3') == 51. Thus, you have bytes255 51 51, which you can confirm by doinglist(a)\x99\x99\x99\xff33\xb3\xff\xcd, which seems rather inconsistent to me. Why python decide to escape some character but display ascii for others... Why not just show everything per byte with \x escape.b'\xff33' == b'\xff\x33\x33'