I am testing struct module because I would like to send simple commands with parameters in bytes (char) and unsigned int to another application.
However I found some weird things when converting to little endian unsigned int, these examples print the correct hexadecimal representation:
>>> import struct
>>> struct.pack('<I',7)
b'\x07\x00\x00\x00'
>>> struct.pack('<I',11)
b'\x0b\x00\x00\x00'
>>> struct.pack('<I',16)
b'\x10\x00\x00\x00'
>>> struct.pack('<I',15)
b'\x0f\x00\x00\x00'
but these examples apparently not:
>>> struct.pack('<I',10)
b'\n\x00\x00\x00'
>>> struct.pack('<I',32)
b' \x00\x00\x00'
>>> struct.pack('<I',64)
b'@\x00\x00\x00'
I would appreciate any explanation or hint. Thanks beforehand!
"\xNN"
is just the way to represent a non-prinatble character ... it will give you the prinable character if it canprint "\x0a" == "\n" == chr(10)
Python is being helpful.
The
bytes
representation will use ASCII characters for any bytes that are printable and escape codes for the rest.Thus, 0x40 is printed as
@
, because that's a printable byte. But0x0a
is represented as\n
instead, because that is the standard Python escape sequence for a newline character. 0x00 is represented as\x00
, a hex escape sequence denoting the NULL byte value. Etc.All this is just the Python representation when echoing the values, for your debugging benefit. The actual value itself still consists of actual byte values.
It's just that any byte in the printable ASCII range will be shown as that ASCII character rather than a
\xhh
hex escape or dedicated\c
one-character escape sequence.If you wanted to see only hexadecimal representations, use the
binascii.hexlify()
function:which returns bytes as hex characters (with no prefixes), instead. The return value is of course no longer the same value, you now have a bytestring of twice the original length consisting of characters representing hexadecimal values, literal
a
through tof
and0
through to9
characters.