En Mon, 23 Feb 2009 22:18:31 -0200, harijay <hari...@gmail.com> escribió:

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" .

No, should be type 'int', that's what the H format means.

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' " .

Try again - it should work. Perhaps you used the wrong variable name? instead of the resultant from struct.unpack

py> import struct
py> element1_format = struct.Struct("<H")
py> (element1,) = element1_format.unpack('AB')
py> element1
16961
py> element1 & 0x007F
65
py> chr(65)
'A'

--
Gabriel Genellina

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to