Python Struct Pack Displaying Ascii
Solution 1:
You are looking at the result of the repr()
function, which the Python interactive interpreter uses on all results that are not None
.
Python string contents are shown using ASCII text for any character that is printable, \r
, \n
and \t
for ASCII carriage return, newline and tab characters respectively, and \xhh
hex escapes for the rest.
And yes, '\x79'
is the exact same byte as 'y'
:
>>> 'y' == '\x79'True
but when producing the representation, Python simply prefers to show you the printable ASCII character:
>>> '\x79''y'
You could encode the string to 'hex'
if you want to see all codepoints represented as hexadecimal:
>>> 'y\x19'.encode('hex')
'7919'
Solution 2:
Yes, the information is the same. The struct is a sequence of bytes, and printable bytes are displayed as the character they represent. The reason one is shown in escape form and the other isn't is that one is a printable ASCII character and the other isn't.
Post a Comment for "Python Struct Pack Displaying Ascii"