Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python struct pack with space padding

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

like image 764
Pradyot Avatar asked Oct 26 '12 15:10

Pradyot


1 Answers

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)
like image 129
Doug T. Avatar answered Sep 18 '22 17:09

Doug T.