Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Parsing Binary Structures; Is there a better way / What is your way?

Reply
Thread Tools

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

 
 
Paul Rubin
Guest
Posts: n/a
 
      08-05-2009
"Martin P. Hellwig" <(E-Mail Removed)> writes:
> what I usually do is read the packet in binary mode, convert the
> output to a concatenated 'binary string'(i.e. '0101011000110') and


Something wrong with reading the data words as an integer and using
old fashioned shifts and masks to get at the bit fields?
 
Reply With Quote
 
 
 
 
Paul Rubin
Guest
Posts: n/a
 
      08-05-2009
"Martin P. Hellwig" <(E-Mail Removed)> writes:
> Is there an advantage using shifts and masks over my kitchen type solution?


Weren't you complaining about the 8-to-1 expansion from turning each bit
to an ascii char?
 
Reply With Quote
 
 
 
 
Jon Clements
Guest
Posts: n/a
 
      08-05-2009
On 5 Aug, 20:41, "Martin P. Hellwig" <(E-Mail Removed)>
wrote:
> Paul Rubin wrote:
> > "Martin P. Hellwig" <(E-Mail Removed)> writes:
> >> Is there an advantage using shifts and masks over my kitchen type solution?

>
> > Weren't you complaining about the 8-to-1 expansion from turning each bit
> > to an ascii char?

>
> Yes you are (of course) right, my 'dream' solution would be something
> that accepts slice indeces on bit level. Your reasoning did reveal some
> flaws in my approach though
>
> --
> MPHhttp://blog.dcuktec.com
> 'If consumed, best digested with added seasoning to own preference.'


Ahh huh -- found it...

ID: DLT119
Desc: datasyzygy misc. development, production and accounts.
Date: 2008-12-09
File: pybits.py
"""
Implements a python level interface to reading bit fields with an
associated type. Fields are accessed via __getitem__ and must be a
valid python name. eg:

class MyStructure:
a = BitField(4, int)
b = BitField(6, int)
c = BitField(2, int)
d = BitField(36, str)
"""

Ummm, seems I left the rest pretty vague, but I'm guessing that's what
you're after, and I'm fairly curious to see what I wrote... mind you,
not too hard to write again now that's jogged my memory! (although
after a couple at the pub, not going to do it tonight!)

Cheers,

Jon.

 
Reply With Quote
 
Dave Angel
Guest
Posts: n/a
 
      08-06-2009
Paul Rubin wrote:
> "Martin P. Hellwig" <(E-Mail Removed)> writes:
>
>> Is there an advantage using shifts and masks over my kitchen type solution?
>>

>
> Weren't you complaining about the 8-to-1 expansion from turning each bit
> to an ascii char?
>
>

One warning to Martin:

If you want your code portable across systems, watch out for
big-endian/little-endian issues, as well as alignment ones. Shift &
mask code tends to be highly specific to a particular endian-ness,
especially if trying to get multiple bits that cross a byte or word
boundary.

Over the years, I know I've seen at least three endian versions for the
same 32bit word. Something like abcd, dcba, and cdab.

One advantage of converting first to bitstrings, is that there's just
the two places to fix, for portability, the conversion from byte array
to bitstring, and the conversion back.

DaveA

 
Reply With Quote
 
Hendrik van Rooyen
Guest
Posts: n/a
 
      08-06-2009
On Wednesday 05 August 2009 20:12:05 Paul Rubin wrote:
> "Martin P. Hellwig" <(E-Mail Removed)> writes:
> > what I usually do is read the packet in binary mode, convert the
> > output to a concatenated 'binary string'(i.e. '0101011000110') and

>
> Something wrong with reading the data words as an integer and using
> old fashioned shifts and masks to get at the bit fields?


There is nothing wrong with that, and in the final analysis, it is what
is used - however, I can sympathize with the OP, as it is much nicer
to just call a named thing than to juggle a bunch of hard coded
shifts and masks.

I think the real point here is sort of meta programming - what
is needed is a way to describe and name the elements of the
structure so that useful python objects are created.

The code I posted for bits does that, and I use it all the
time - it is almost as good as being back in 8031 assembler
with direct named bit access.

- Hendrik
 
Reply With Quote
 
Hendrik van Rooyen
Guest
Posts: n/a
 
      08-06-2009
On Wednesday 05 August 2009 21:41:26 Martin P. Hellwig wrote:

> Yes you are (of course) right, my 'dream' solution would be something
> that accepts slice indeces on bit level. Your reasoning did reveal some
> flaws in my approach though


This is the first time I have been compared to the sandman...



- Hendrik

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: thanks very much indeed for your help is there a better way to dothis (python3) newby Rhodri James Python 0 02-24-2009 01:29 AM
Are there better ADSL modem routers out there? Bypass UK VOIP 28 01-12-2007 12:41 AM
Is splint really better than lint? Is there a better tool than splint? Peter Bencsik C Programming 2 09-21-2006 10:02 PM
Build a Better Blair (like Build a Better Bush, only better) Kenny Computer Support 0 05-06-2005 04:50 AM
Better way to do parsing? =?ISO-8859-1?Q?Andr=E9_Roberge?= Python 3 05-04-2005 02:53 PM



Advertisments