Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3 struct.pack() printing weird characters

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!

like image 749
MithPaul Avatar asked Dec 19 '22 19:12

MithPaul


2 Answers

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. But 0x0a 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.

>>> b'\x40' == b'@'
True
>>> b'\x0a' == b'\n'
True

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:

>>> import binascii
>>> binascii.hexlify(b'@\x00\x00\x00')
b'40000000'
>>> binascii.hexlify(b'\n\x00\x00\x00')
b'0a000000'

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 to f and 0 through to 9 characters.

like image 152
Martijn Pieters Avatar answered Jan 04 '23 09:01

Martijn Pieters


"\xNN" is just the way to represent a non-prinatble character ... it will give you the prinable character if it can

print "\x0a" == "\n" == chr(10)

like image 20
Joran Beasley Avatar answered Jan 04 '23 10:01

Joran Beasley