Assign variable length value to a String within a Struct

38 views
Skip to first unread message

Patrick Olsen

unread,
Jan 27, 2016, 1:38:35 PM1/27/16
to Construct
I have the following struct

TEST_STRUCT = construct.Struct(
    u'item_data_struct',
    construct.ULInt32(u'string_length'),
    construct.Bytes(u'unknown', 4),
    construct.String(name, ?????)
)

Returns:

Container:
    string_length = 12
    unknown = '\x01\x01\x00\x00'

So the string is 12 bytes.  I want to be able to assign this value to the next set of N bytes within my struct.

Basically I want it to be...

TEST_STRUCT = construct.Struct(
    u'item_data_struct',
    construct.ULInt32(u'string_length'),
    construct.Bytes(u'unknown', 4),
    construct.String(u'string', construct.ULInt32(u'string_length'))  # Which in this example would be 12.
)

Patrick Olsen

unread,
Jan 27, 2016, 1:46:18 PM1/27/16
to Construct
Figured it out....

TEST_STRUCT = construct.Struct(
    u'item_data_struct',
    construct.ULInt32(u'string_length'),
    construct.Bytes(u'unknown', 4),
    construct.String('string_name', lambda ctx: ctx.string_length)
)

Arek Bulski

unread,
Sep 26, 2016, 10:35:23 AM9/26/16
to Construct
Bytes does not work with unicode strings. Perhaps it was supposed to be a Const with byes literal?
Reply all
Reply to author
Forward
0 new messages