2

What is a pad byte (x) in the python struct type? 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?

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'
asked Oct 27, 2019 at 3:14
1
  • I don't know that it's always zero, but I can answer the rest Commented Oct 27, 2019 at 3:21

1 Answer 1

1

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. This guarantees that, lets say the first 20 bytes are a header, with bytes 0-8 being the message size.

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.

answered Oct 27, 2019 at 3:21
Sign up to request clarification or add additional context in comments.

1 Comment

Looks like the bytes are actually unset in struct.pack, at least for CPython

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.