Python 3 struct.pack() 打印奇怪的字符 [英] Python 3 struct.pack() printing weird characters

查看:102
本文介绍了Python 3 struct.pack() 打印奇怪的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试 struct 模块,因为我想将带有字节 (char) 和 unsigned int 参数的简单命令发送到另一个应用程序.

但是我在转换为 little endian unsigned int 时发现了一些奇怪的东西,这些示例打印了正确的十六进制表示:

<预><代码>>>>导入结构>>>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'

但这些例子显然不是:

<预><代码>>>>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'

我希望得到任何解释或提示.先谢谢了!

解决方案

Python 很有帮助.

bytes 表示将对任何可打印字节使用 ASCII 字符,其余字节使用转义码.

因此,0x40 被打印为 @,因为这是一个可打印的字节.但是 0x0a 被表示为 \n ,因为这是换行符的标准 Python 转义序列.0x00 表示为 \x00,一个表示 NULL 字节值的十六进制转义序列.等

所有这些只是回显值时的 Python 表示,以便您进行调试.实际值本身仍然由实际字节值组成.

<预><代码>>>>b'\x40' == b'@'真的>>>b'\x0a' == b'\n'真的

只是可打印 ASCII 范围内的任何字节都将显示为该 ASCII 字符,而不是 \xhh 十六进制转义符或专用的 \c 单字符转义序列.

如果您想查看十六进制表示,请使用 binascii.hexlify() 函数:

<预><代码>>>>导入二进制文件>>>binascii.hexlify(b'@\x00\x00\x00')b'40000000'>>>binascii.hexlify(b'\n\x00\x00\x00')b'0a000000'

以十六进制字符(没有前缀)的形式返回字节.返回值当然不再是相同的值,您现在有一个两倍于原始长度的字节串,由表示十六进制值的字符组成,文字 af09 个字符.

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!

解决方案

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.

这篇关于Python 3 struct.pack() 打印奇怪的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆