Bitpacked Data

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • none

    #1

    Bitpacked Data

    i need to interface python with a bitpacked data file,
    the structure recorded in the file is the following:

    struct {
    var_1 4bit
    var_2 6bit
    var_3 2bit
    var_3 4bit
    }

    how can read the struct and convert data into dictionary

    Thnx
  • Bjoern Schliessmann

    #2
    Re: Bitpacked Data

    none wrote:
    i need to interface python with a bitpacked data file,
    the structure recorded in the file is the following:
    >
    struct {
    var_1 4bit
    var_2 6bit
    var_3 2bit
    var_3 4bit
    }
    Strange data types. What language is this?
    how can read the struct and convert data into dictionary
    I'd use the module struct, with bit shifting operators to extract
    the bits.


    Regards,


    Björn

    --
    BOFH excuse #232:

    Ionization from the air-conditioning

    Comment

    • bearophileHUGS@lycos.com

      #3
      Re: Bitpacked Data

      Bjoern Schliessmann:
      I'd use the module struct, with bit shifting operators to extract
      the bits.
      Maybe Pyrex can be used to create a small module that can manage
      similar bitfields quickly and with a syntax not too much far from the
      Erlang bit syntax.

      Bye,
      bearophile

      Comment

      • Nick Vatamaniuc

        #4
        Re: Bitpacked Data

        On Mar 11, 5:49 pm, none <""luca\"@(none )"wrote:
        i need to interface python with a bitpacked data file,
        the structure recorded in the file is the following:
        >
        struct {
        var_1 4bit
        var_2 6bit
        var_3 2bit
        var_3 4bit
        >
        }
        >
        how can read the struct and convert data into dictionary
        >
        Thnx
        I noticed that you have a 4+6+2+4=16 bits total. That's a short
        (unsigned) integer that you'd have to unpack using the stuct module's
        unpack method.

        The format string would be 'H'. Unpack using big endian. Here is a
        quick example for you:
        ------------------------------
        vatamane@nthink :~$ ipython
        >>from struct import pack,unpack
        >>m1='1'*4
        >>m2='1'*6
        >>m3='1'*2
        >>m4='1'*4
        >>m1,m2,m3,m4
        ('1111', '111111', '11', '1111')
        >>#define some data
        >>idata=int('11 00101011111110' ,2)
        >>idata
        51966
        >>#pack as a 16 bit integer (usigned int)
        >>packed=pack(' >H',idata)
        >>#note means big endian
        >>packed
        '\xca\xfe'
        >>#oh, wow it spells cafe! ;-)
        >>#now unpack
        >>unpacked=unpa ck('>H',packed)
        >>unpacked
        (51966,)
        >>odata=unpacke d[0]
        >>vars=[]
        >>for mask in (m4,m3,m2,m1):
        ....: vars.append(oda ta & int(mask,2) )
        ....: odata>>=len(mas k)
        ....:
        >>vars
        [14, 3, 43, 12]
        >>vars.reverse( )
        >>vars
        [12, 43, 3, 14]

        -------------------------------------------------
        So in binary v1='1100' v2='101011' v3='11' v4='1110'
        If you concatenate them: 110010101111111 0 you get the original
        idata...

        Hope this helped,
        Nick Vatamaniuc




        Comment

        • Nick Craig-Wood

          #5
          Re: Bitpacked Data

          none <""wrote:
          i need to interface python with a bitpacked data file,
          the structure recorded in the file is the following:
          >
          struct {
          var_1 4bit
          var_2 6bit
          var_3 2bit
          var_3 4bit
          }
          >
          how can read the struct and convert data into dictionary
          You could try Construct



          This allows you to build a python class which will translate to and
          from that datastructure.

          --
          Nick Craig-Wood <nick@craig-wood.com-- http://www.craig-wood.com/nick

          Comment

          Working...