Parsing Binary Structures; Is there a better way / What is your way?

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Thu Aug 6 01:50:01 EDT 2009


En Wed, 05 Aug 2009 11:46:13 -0300, Martin P. Hellwig  
<martin.hellwig at dcuktec.org> escribió:

> On several occasions I have needed (and build) a parser that reads a  
> binary piece of data with custom structure. For example (bogus one):
>
> BE
> +---------+---------+-------------+-------------+------+--------+
> | Version | Command | Instruction | Data Length | Data | Filler |
> +---------+---------+-------------+-------------+------+--------+
> Version: 6 bits
> Command: 4 bits
> Instruction: 5 bits
> Data Length: 5 bits
> Data: 0-31 bits
> Filler: filling 0 bits to make the packet dividable by 8

> - Using a string for binary representation takes at least 8 times more  
> memory for the packet than strictly necessary.

The size difference isn't so big; an integer takes 12 bytes and a string  
takes 24+len bytes. "Data" above would take 56 bytes max when stored as a  
string '0110001...' vs. 12 bytes when using a plain integer (sizes  
computed on Windows 32bits). Plus, doing bitwise operations in Python  
isn't a fast operation as it is in C, by example -- so your current  
implementation might be a quite good one (in pure Python, I mean).

-- 
Gabriel Genellina




More information about the Python-list mailing list