python 结构体、网络应用程序的字节顺序和对齐方式以及 unsigned int 和 unsigned long 之间的区别

发布于 2024-11-25 22:40:46 字数 504 浏览 8 评论 0 原文

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

字节顺序、大小和对齐方式:对于网络通信,我应该使用 ! 而不是 = 吗?
例如: struct.pack("!BBH", 1, 12, 512) 或 struct.pack("=BBH", 1, 12, 512) ?

格式字符 :unsigned int (I) 之间有什么区别和 unsigned long (L),因为两者都是 4 个字节?

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

I have 2 questions about struct in the python documentation:

Byte Order, Size, and Alignment : For Network communication should i use ! instead of = ?
eg: struct.pack("!BBH", 1, 12, 512) or struct.pack("=BBH", 1, 12, 512) ?

Format characters : What is the difference between unsigned int (I) and unsigned long (L), since both are 4 bytes?

Thank you for your time, and forgive me if its a silly question :-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素手挽清风 2024-12-02 22:40:46

如果您希望代码无论在何处运行都产生相同的输出,为什么要曾经使用=?当然,是的,您应该使用对每个人都具有相同含义的代码。标准是 !,因此请使用 !

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

“标准大小”列是指使用标准大小时打包值的大小(以字节为单位);也就是说,当格式字符串以 '<'、'>'、'!' 之一开头时或“=”。使用本机大小时,打包值的大小取决于平台。

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

If you want your code to produce the same output wherever it is run, why would you ever use =? So of course, yes, you should use a code that means the same thing to everyone. The standard is ! so use !.

Read the header above the format code table:

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.

So, if you use the @ format code or omit the format code, the size of I and the size of L could theoretically be different, depending on your platform.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文