开发者

python struct, Byte order and Alignment for Network application and difference between unsigned int and unsigned long

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) an开发者_高级运维d unsigned long (L), since both are 4 bytes?

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜