packing and unpacking variable length array/string using the struct module in python
The struct
module does only support fixed-length structures. For variable-length strings, your options are either:
Dynamically construct your format string (a
str
will have to be converted to abytes
before passing it topack()
):s = bytes(s, 'utf-8') # Or other appropriate encodingstruct.pack("I%ds" % (len(s),), len(s), s)
Skip
struct
and just use normal string methods to add the string to yourpack()
-ed output:struct.pack("I", len(s)) + s
For unpacking, you just have to unpack a bit at a time:
(i,), data = struct.unpack("I", data[:4]), data[4:]s, data = data[:i], data[i:]
If you're doing a lot of this, you can always add a helper function which uses calcsize
to do the string slicing:
def unpack_helper(fmt, data): size = struct.calcsize(fmt) return struct.unpack(fmt, data[:size]), data[size:]
I've googled up this question and a couple of solutions.
construct
An elaborate, flexible solution.
Instead of writing imperative code to parse a piece of data, you declaratively define a data structure that describes your data. As this data structure is not code, you can use it in one direction to parse data into Pythonic objects, and in the other direction, convert (“build”) objects into binary data.
The library provides both simple, atomic constructs (such as integers of various sizes), as well as composite ones which allow you form hierarchical structures of increasing complexity. Construct features bit and byte granularity, easy debugging and testing, an easy-to-extend subclass system, and lots of primitive constructs to make your work easier:
Updated: Python 3.x, construct 2.10.67; also they have native PascalString, so renamed
from construct import * myPascalString = Struct( "length" / Int8ul, "data" / Bytes(lambda ctx: ctx.length) ) >>> myPascalString.parse(b'\x05helloXXX') Container(length=5, data=b'hello') >>> myPascalString.build(Container(length=6, data=b"foobar")) b'\x06foobar' myPascalString2 = ExprAdapter(myPascalString, encoder=lambda obj, ctx: Container(length=len(obj), data=obj), decoder=lambda obj, ctx: obj.data ) >>> myPascalString2.parse(b"\x05hello") b'hello' >>> myPascalString2.build(b"i'm a long string") b"\x11i'm a long string"
ed: Also pay attention to that ExprAdapter, once native PascalString won't be doing what you need from it, this is what you will be doing.
netstruct
A quick solution if you only need a struct
extension for variable length byte sequences. Nesting a variable-length structure can be achieved by pack
ing the first pack
results.
NetStruct supports a new formatting character, the dollar sign ($). The dollar sign represents a variable-length string, encoded with its length preceeding the string itself.
edit: Looks like the length of a variable-length string uses the same data type as the elements. Thus, the maximum length of variable-length string of bytes is 255, if words - 65535, and so on.
import netstruct>>> netstruct.pack(b"b$", b"Hello World!")b'\x0cHello World!'>>> netstruct.unpack(b"b$", b"\x0cHello World!")[b'Hello World!']
An easy way that I was able to do a variable length when packing a string is:
pack('{}s'.format(len(string)), string)
when unpacking it is kind of the same way
unpack('{}s'.format(len(data)), data)