struct.pack 包装不紧

struct.pack doesn't pack tightly

我想像这样创建并稍后发送一个 5 字节结构:

import struct
struct.pack("?i", True, 0x01020304)
>>b'\x01\x00\x00\x00\x04\x03\x02\x01'

但是如您所见,1 字节的布尔值被填充了 3 个字节或出于某种原因填充为整数。 结果我想要的是:

>>b'\x01\x04\x03\x02\x01'

我该怎么做?为什么我的解决方案不起作用?根据 the documentation.

似乎是正确使用的

. The = character at the start of the format string tells the pack method to not align the data but to produce a byte string of exactly the specified length. Whether a format character allows alignment or not is specified in this chapter 的回答解决了我的问题。不知何故我错过了。