简体   繁体   English

python 结构中的填充字节

[英]Pad byte in python struct

What is a pad byte (x) in the python struct type? python struct类型中的pad byte (x) 是什么? Is it an unsigned char with value 0 , or what exactly does it look like / why is it one of the types that are available in the struct object?它是值为0的无符号字符,还是它到底是什么样子/为什么它是struct object 中可用的types之一?

For example, what would be the difference between doing one of the following:例如,执行以下操作之一有什么区别:

>>> struct.pack('BB', 0, ord('a'))
b'\x00a'
>>> struct.pack('xB', ord('a'))
b'\x00a'

It's useful for matching required length of another system.它对于匹配另一个系统所需的长度很有用。

For example.例如。 In my work, there is a server that sends a fixed sized header and expects fixed sized messages.在我的工作中,有一个服务器发送固定大小的 header 并期望固定大小的消息。 This guarantees that, lets say the first 20 bytes are a header, with bytes 0-8 being the message size.这保证了,假设前 20 个字节是 header,字节 0-8 是消息大小。

It doesn't really matter what type the pad is.垫子是什么类型并不重要 It's basically junk data.基本上都是垃圾数据。 unsigned char 0 is a good choice though and the one that struct.pack uses. unsigned char 0 是一个不错的选择,也是 struct.pack 使用的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM