Can someone identify what this notation for these bytes
is? At first glance, I tend to think "hexadecimal", but I don't recognize what things like xf1Y
and e1fl
are:
b'vy\xe9\xb5\xa2\xba\xf1Y\xe8\xe1fl\x1d\x87\xacC'
I get this when I encode things using some_text.encode('utf-8')
.
I am trying to get bytes that I can pass to cryptography methods that worked with Python 2's byte strings.
You're right – it's a hexadecimal notation.
In a bytes literal, any byte which can't be represented by a printable ASCII character (or one of the standard escapes \n
, \t
or \r
) is represented as \xNN
, where NN is the 2-digit hexadecimal representation of the byte.
What's confusing you is that you're mistaking e.g. \xf1Y
for a single escape sequence, when in fact it represents two separate bytes:
>>> len(b'\xf1Y')
2
>>> [bytes([b]) for b in b'\xf1Y']
[b'\xf1', b'Y']
If you iterate over a bytes object, you'll get the integer values of the bytes back:
>>> list(b'vy\xe9\xb5\xa2\xba\xf1Y\xe8\xe1fl\x1d\x87\xacC')
[118, 121, 233, 181, 162, 186, 241, 89, 232, 225, 102, 108, 29, 135, 172, 67]
>>> bytes([118])
b'v'
>>> bytes([121])
b'y'
>>> bytes([233])
b'\xe9'
The documentation for escape sequences in Python string and bytes objects has a little more information on escape sequences Python understands (although those above are the only ones it uses to represent bytes objects).
来源:https://stackoverflow.com/questions/41421501/strange-notation-for-python-3-bytes