I need to create/send binary data in python using a given protocol. The protocol calls for fixed width fields , with space padding thrown in. Using python's struct.pack, the only thing I can think of is, calculating the space padding and adding it in myself. Is there a better way to achieve this?
thanks
struct has a placeholder (x
) for a padding byte you can use:
# pack 2 16 bit values plus one pad byte
from struct import pack
packedStrWithOneBytePad = pack("hhx", 1000, 2000)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With