Python 3 struct.pack() printing weird characters

倾然丶 夕夏残阳落幕 提交于 2020-03-14 18:41:40

问题


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!


回答1:


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.




回答2:


"\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)



来源:https://stackoverflow.com/questions/21052068/python-3-struct-pack-printing-weird-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!