Python int转换为字节(打包/解压缩)

我正在尝试构建一条消息并将其发送到C套接字服务器。

从此C服务器的测试中,我可以看到它如何序列化消息,例如:

0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0xc8, // 100 200

So first 4 bytes for first number(100) and next 4 bytes for 200. It looks like i need to use struct to encode/decode this number but i have this weird problem:

# Unpack value to check that this is right format
>>> struct.unpack('>I', b'\x00\x00\x00\x64')
(100,)

# Now try to pack 100
>>> struct.pack('>I', 100)
b'\x00\x00\x00d'

如您所见,我可以成功地打包价值并获得正确的结果,但是我无法打包。为什么不同?如何建立完全相同的结构?

第二个问题几乎相同,但类型不同:

0x04, 0x38, 0x07, 0x80, // 1080 1920
>>> struct.unpack('>h', b'\x04\x38')
(1080,)
>>> struct.pack('>h', 1080)
b'\x048'