pythonbytebufferunpack

What's the recommended "pythonic way" for unpacking a byte encoded message with arbitrary bit length fields?


Suppose I have a byte-packed message with the following schema:

Message Name: Ball
    Field #1: 
        Name: Color
        Type: unsigned int
        Start Bit: 0
        Bit Length: 4
    Field #2: 
        Name: Radius
        Type: single
        Start Bit: 4
        Bit Length: 32
    Field #3: 
        Name: New
        Type: bool
        Start Bit: 36
        Bit Length: 1

What is the recommended pythonic way to convert the byte sequence into a python variables? Would the "struct" module work well for unpacking byte arrays that have arbitrary bit length fields?


Solution

  • What is the recommended pythonic way to convert the byte sequence into a python variables?

    What you want is to read this python doc page about struct and bytearrays.

    It will show you how to pack and unpack data using format.

    Would the "struct" module work well for unpacking byte arrays that have arbitrary bit length fields?

    It will show you how to pack and unpack data using format.

    Yup, as follows:

    import struct
    color, radius, new = struct.unpack("If?", incoming_bytes)
    

    Check the format characters to define the format string and you're done.

    You could use more bloated libraries like construct, but TBH, this format is super simple and if you unpack early and pack late, you're free to organize your data as you want in your code.

    e.g.:

    class Ball:
        def __init__(self, color, radius, new):
            self.color = color
            self.radius = radius
            self.new = new
    
    Ball(*unpack("If?", incoming_bytes))