How to Nest Structs?

A

Ari King

Hi,

I'm trying to nest the "info_header", "info_body", and "info_trailer" structs (see below) into a "data_packet" struct. Does anyone know how I can/should accomplish this? Thanks.

batch_header_format = struct.Struct('!c2h')
info_header_format = struct.Struct('!2hl')
mkt_status_format = struct.Struct('!c')
info_trailer_format = struct.Struct('!hc')

mkt_session_codes = [b'PO',b'PC',b'CO',b'CC',b'CK',b'CL']
mkt_type = [b'N',b'S',b'O',b'A',b'C',b'G']

batch_header = batch_header_format.pack(b'1',1,1024)

total_size = info_header_format.size + mkt_status_format.size + info_trailer_format.size

# Combine following into data struct.
info_header = info_header_format.pack(int(binascii.hexlify(mkt_session_codes[random.randint(0,5)])), total_size, 124)
info_body = mkt_status_format.pack(mkt_type[random.randint(0,5)])
info_trailer = info_trailer_format.pack(0, b'\r')

-Ari
 
W

web

Try unpacking the nested struct as a fixed width string and then unpacking the string.

Then unpack the string
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top