网络应用的python结构、字节顺序和对齐方式以及无符号整型与无符号long的区别

2024-10-06 12:32:22 发布

您现在位置:Python中文网/ 问答频道 /正文

在python文档中,我有两个关于struct的问题:

Byte Order, Size, and Alignment:对于网络通信,我应该使用!而不是{}?
例如:struct.pack("!BBH", 1, 12, 512)还是{}?在

Format characters:无符号int(I)和unsigned long(L)之间有什么区别,因为它们都是4字节?在

感谢您抽出时间,如果这是一个愚蠢的问题,请原谅我:-)


Tags: and文档formatsize符号orderbytestruct
1条回答
网友
1楼 · 发布于 2024-10-06 12:32:22

如果您希望您的代码在任何地方运行都能产生相同的输出,那么您为什么要使用=?所以当然,是的,你应该使用一个对每个人都有相同意义的代码。标准是!,所以使用!。在

阅读格式代码表上方的标题:

The ‘Standard size’ column refers to the size of the packed value in bytes when using standard size; that is, when the format string starts with one of '<', '>', '!' or '='. When using native size, the size of the packed value is platform-dependent.

因此,如果使用@格式代码或省略格式代码,I的大小和{}的大小理论上可能不同,这取决于您的平台。在

相关问题 更多 >