1

In Python 3, lets say I have a string of bytes:

my_byte_string = b'\x07L\x1b\xa8b\x8f\xba\x1b{\x1c\x16CwW\xa5\xae!\x93MY'

and I use the base64 module to get this:

my_base64_string = base64.b64encode(my_byte_string)

print(my_base64_string)

b'B0wbqGKPuht7HBZDd1elriGTTVk='

When I do this, I am confused about what happens to the '\' characters. I know that they indicate separation of different bytes, but are they being encoded by b64encode like the other characters, or are they being ignored?

1
  • You are confusing embedded special characters with real characters. A simpler example would be what happens to \n - that is not stored as two characters but as the character equivalent of a newline for your platform. Commented Jun 3, 2017 at 16:21

1 Answer 1

2

The '\' character doesn't indicate the separation of different bytes, it is a string literal, which is used to escape characters with special meaning. In your case, you use \x<hex>, which tells the Python interpreter to place the byte value you specified into the String. Thus, these bytes that you sequentially specify (0x07L, 0x1b, ...) are encoded to base64, and nothing else.

To verify this, look at the hexadecimal representation of your binary data:

>>> import binascii
>>> binascii.hexlify(my_byte_string)
b'074c1ba8628fba1b7b1c16437757a5ae21934d59'
Sign up to request clarification or add additional context in comments.

Comments