struct module : getting at individual bits inside byte field

Discussion in 'Perl Misc' started by harijay, Feb 23, 2009.

  1. harijay

    harijay Guest

    In my last post I had asked about reading data from a binary file
    using the struct module.
    Thanks to some excellent help , I have managed to read in successfully
    most of the header . These are some time-voltage traces from a digital
    to analog converter for my experiments output according to a format
    mentioned here : ( http://www.dataq.com/support/techinfo/ff.htm)

    I have a question about how to bitmask a bunch of bytes read in from
    such a binary formatted file .

    For eg the spec says the first two bytes have different parameters in
    different bits .
    Byte 1 Byte 0
    SN16 SD9 SD8 SD7 SD6 SD5 SD4 SD3 SD2 SD1 SD0 T4 T3 T2 T1 T0

    I am reading in the two bytes using the following code
    import struct
    f.seek(0)
    element1_format = struct.Struct("<H")
    (element1,) = element1_format.unpack(f.read(2))

    Now element1 has type "str" . How do I apply a bitmask to this to get
    at information in the component bits .

    Since the entire file format has many such bitmasked fields and since
    this is my first venture into binary formats and c-type structs , I
    wanted to know how to read values inside a byte using python.

    My few tries at using bitwise operators ( element1 & 0x001f) are
    frustrated by messages that say " unsupported operand type(s) for &:
    'str' and 'int' " .

    How can I keep my string objects as bits and apply bitmasks to them

    Any help in this will be greatly appreciated.
    Thanks
    hari
    harijay, Feb 23, 2009
    #1
    1. Advertising

  2. harijay

    Marc Lucksch Guest

    harijay schrieb:
    > import struct
    > Now element1 has type "str" .
    > ..using python.
    > " unsupported operand type(s) for &: 'str' and 'int' " .


    I'm going out on a limb here and guess this ain't really perl related,
    so it's the wrong newsgroup.
    Marc Lucksch, Feb 24, 2009
    #2
    1. Advertising

  3. harijay

    harijay Guest

    On Feb 23, 7:07 pm, Marc Lucksch <> wrote:
    > harijay schrieb:
    >
    > > import struct
    > > Now element1 has type "str" .
    > > ..using python.
    > > " unsupported operand type(s) for &: 'str' and 'int' " .

    >
    > I'm going out on a limb here and guess this ain't really perl related,
    > so it's the wrong newsgroup.


    Whoops ..wrong newsgroup
    Sorry perl folks.
    harijay, Feb 24, 2009
    #3
    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. benn
    Replies:
    5
    Views:
    516
    Joseph Kesselman
    Mar 20, 2008
  2. Hybr1dz
    Replies:
    6
    Views:
    2,447
    Hybr1dz
    Apr 2, 2008
  3. harijay
    Replies:
    4
    Views:
    448
    harijay
    Feb 24, 2009
  4. Daku
    Replies:
    6
    Views:
    6,555
  5. googler

    Accessing individual bits in a number

    googler, May 19, 2008, in forum: Perl Misc
    Replies:
    0
    Views:
    106
    googler
    May 19, 2008
Loading...

Share This Page