[lang]

Present Perfect

Personal
Projects
Packages
Patches
Presents
Linux

Picture Gallery
Present Perfect

Python’s struct.unpack

Filed under: Hacking,Python — Thomas @ 10:10 am

2009-4-14
10:10 am

Is it just me, or does struct.unpack seem mostly useless to unpack any real-world binary data ? Real-world binary data usually has a fixed amount of bytes for each value to read out of a binary file. struct.unpack does not allow you to specify how many bytes to read for each value to unpack; rather, it has format specifiers that map to the C type declarations.

The only way I found that works to read, for example, sets of 32 bit signed integers, is to read them with one call per variable, and slicing the data, like so:

self.trackCount = struct.unpack("B", data[0])[0]
self.discId1 = "%08x" % struct.unpack("

Surely there's an obvious better way to do this ?

NOTE: for some reason WordPress does not allow me to have the first <L format string to show up as just that; instead it corrects it to '<I ' (different letter, and a space). FML.

15 Comments »

  1. You can use struct.unpack_from that take an offset, and only require that len(buffer[offset:]) >= struct.calcsize(fmt).

    Your sample would thus be rewritten as:
    self.trackCount = struct.unpack_from(“B”, data, 0)[0]
    self.discId1 = “%08x” % struct.unpack_from(“<l “, data, 1)[0]
    self.discId2 = “%08x” % struct.unpack_from(“<L”, data, 5)[0]

    Comment by sdefresne — 2009-4-14 @ 10:35 am

  2. What is wrong with

    (self.trackCount, self.discId1, self.diskId2) = struct.unpack(”<BlL”, data[:9])

    Comment by Guillaume — 2009-4-14 @ 11:09 am

  3. @Guillaume: what guarantees that struct.unpack reads an ‘L’ as a 32-bit integer, and not a 64-bit integer ?

    Comment by Thomas — 2009-4-14 @ 11:17 am

  4. See http://python.org/doc/current/library/struct.html might help, in particular, the paragraphs explaining what “standard size” means for int and long fields, plus the table indicating that “<” forces the standard size interpretation.

    Comment by Marius Gedminas — 2009-4-14 @ 11:30 am

  5. @Thomas: because the documentation says L is a 32bits integer ?

    Comment by tahorg — 2009-4-14 @ 11:36 am

  6. @Thomas: same than tahorg here. Please notice the difference between the “native” and “standard” mode.

    Comment by Guillaume — 2009-4-14 @ 1:12 pm

  7. If you have multiple or many values to unpack, the question is: what sort of data structure do you want to unpack these into? In most cases, I doubt you want the flat tuple that unpack provides. You probably want to unpack the data into an array structure. Both the stdlib array module and numpy permit this (using the same type-codes as struct).

    Comment by BC — 2009-4-14 @ 3:58 pm

  8. It gets successively more and more messy, if you have structs inside of structs. Really, it would be great if there was some sort of grouping inside the struct format.

    Comment by Edward Z. Yang — 2009-4-14 @ 4:41 pm

  9. Only a learner here, but I’ve been looking at diveintopython.org (I think that’s the site) and this looks similar to the MP3 ID3v1 tag example on there…

    Comment by TGM — 2009-4-14 @ 4:46 pm

  10. self.trackCount, self.discId1, self.discId2 = struct.unpack(“<BLL”, data)
    self.discId1 = “%08x” % self.discId1
    self.discId2 = “%08x” % self.discId2

    Comment by ΤΖΩΤΖΙΟΥ — 2009-4-14 @ 6:48 pm

  11. One other benefit of struct.unpack_from is that it doesn’t require any string copying to extract data from part of a larger byte string.

    If you’re stuck on Python 2.4, you can emulate it with something like:

    def unpack_from(fmt, buf, offset=0):
    slice = buffer(buf, offset, struct.calcsize(fmt))
    return struct.unpack(fmt, slice)

    And as others have said, it will be more efficient if you unpack as much data as you can in one go rather than lots of little calls.

    Comment by James Henstridge — 2009-4-15 @ 3:54 am

  12. The Hachoir core library might be a useful alternative to the struct module.

    Comment by Jonathan — 2009-4-24 @ 12:01 pm

  13. That’s http://hachoir.org/

    Comment by Jonathan — 2009-4-24 @ 12:05 pm

  14. You can have even more fun if you construct the unpack string on the fly…

    Comment by Gadget Steve — 2010-6-1 @ 3:17 pm

  15. Complete Python code example for implementing struct unpack is posted at following reference.
    >> magic = struct.unpack(“<14b",test)
    refer http://www.fullchipdesign.com/read_binary_rb_bin_struct_unpack.htm

    Comment by Atul — 2010-8-31 @ 2:30 am

RSS feed for comments on this post. TrackBack URL

Leave a comment

picture