Riskhan
Riskhan

Reputation: 4470

Error in packing 128 byte structure using Struct in python

I want to pack 128 byte of different data types. The structure as follows

4 bytes - 0x12345678,

2 bytes - 0x1234,

120 bytes - 0x00 (repeats 120 times),

2 byte - 0x99 ,

I tried with below code but fails

struct.pack('<LH120BH',0x12345678,0x1234,0x00,0x99 )

gives error

Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
struct.pack('<LH120BH',0x12345678,0x1234,0x00,0x99 )
struct.error: pack expected 123 items for packing (got 4)

pls help me. Thanks in advane

Upvotes: 1

Views: 420

Answers (1)

ExplodingGayFish
ExplodingGayFish

Reputation: 2897

You may need to pack 0x00 in to an array if you want it to repeat 120 times and unpack it when call struct.pack, maybe something like this:

struct.pack('<LH120BH',0x12345678,0x1234,*[0x00] * 120,0x99)

Upvotes: 1

Related Questions