struct.pack的奇怪结果?

Che*_*ngy 0 python

在搞乱struct.pack()和socket.htons()时,我得到了一些对我来说没有意义的结果......

>>> struct.pack("h", socket.htons(80))
'\x00P'
>>> struct.pack("h", socket.htons(81))
'\x00Q'
>>> struct.pack("h", socket.htons(82))
'\x00R'
Run Code Online (Sandbox Code Playgroud)

P,Q和R来自哪里,它们是什么意思?在文档中找不到任何关于它们的信息.

fal*_*tru 10

ASCII代码P,Q,R80,81,82.

>>> ord('P')
80
Run Code Online (Sandbox Code Playgroud)

在交互式shell中,可打印字符将自行打印,而不是使用该\xhh格式进行转义.

>>> hex(80)
'0x50'
>>> '\x50'
'P'
>>> '\xff'
'\xff'
Run Code Online (Sandbox Code Playgroud)