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

Discussion in 'Python' started by Paul Rubin, Aug 5, 2009.

  1. Paul Rubin

    Paul Rubin Guest

    "Martin P. Hellwig" <> 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?
    Paul Rubin, Aug 5, 2009
    #1
    1. Advertising

  2. Paul Rubin

    Paul Rubin Guest

    "Martin P. Hellwig" <> 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?
    Paul Rubin, Aug 5, 2009
    #2
    1. Advertising

  3. Paul Rubin

    Jon Clements Guest

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

    On 5 Aug, 20:41, "Martin P. Hellwig" <>
    wrote:
    > Paul Rubin wrote:
    > > "Martin P. Hellwig" <> 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.
    Jon Clements, Aug 5, 2009
    #3
  4. Paul Rubin

    Dave Angel Guest

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

    Paul Rubin wrote:
    > "Martin P. Hellwig" <> 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
    Dave Angel, Aug 6, 2009
    #4
  5. Re: Parsing Binary Structures;Is there a better way / What is your way?

    On Wednesday 05 August 2009 20:12:05 Paul Rubin wrote:
    > "Martin P. Hellwig" <> 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
    Hendrik van Rooyen, Aug 6, 2009
    #5
  6. Re: Parsing Binary Structures;Is there a better way / What is your way?

    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
    Hendrik van Rooyen, Aug 6, 2009
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tweak
    Replies:
    14
    Views:
    2,760
    Eric Sosman
    Jun 11, 2004
  2. Alfonso Morra
    Replies:
    11
    Views:
    701
    Emmanuel Delahaye
    Sep 24, 2005
  3. Peter Bencsik
    Replies:
    2
    Views:
    809
  4. Rhodri James
    Replies:
    0
    Views:
    661
    Rhodri James
    Feb 24, 2009
  5. Replies:
    2
    Views:
    42
    Mark H Harris
    May 13, 2014
Loading...

Share This Page