0

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

7
  • \x is an escape sequence, but the other characters just represent the bytes corresponding to their ascii value. Commented Sep 28, 2020 at 21:55
  • So consider a, the the escape sequence \xff represents the byte with value 255, i.e. "ff" in hexadecimal. However, 3 and 3 each 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 bytes 255 51 51, which you can confirm by doing list(a) Commented Sep 28, 2020 at 22:01
  • Ah so \x only escape the first byte, and the rest of each char is an ascii value? That makes a lit of sense now. Commented Sep 28, 2020 at 22:03
  • I have a mmap read from a file that read out like this \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. Commented Sep 28, 2020 at 22:06
  • Yes, you have to escape the bytes that are outside of the ascii range, of course, you can still use escape sequences for the whole thing, in other words, b'\xff33' == b'\xff\x33\x33' Commented Sep 28, 2020 at 22:06

1 Answer 1

1

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.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.