By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,957 Members | 1,960 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,957 IT Pros & Developers. It's quick & easy.

Binary handling

P: n/a
Hello all,
I have a file of binary data. I want to read this file and parse the data in
it. The format of the file is:
8 bits number of data pakets
6 bits offset
20 bits time
8 states

How can I read this fields in binary? I see the struct module and the
binascii, but I can't split the diferent fiels.

Thanks in advance
Regards
--
Defel
Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Derfel wrote:
Hello all,
I have a file of binary data. I want to read this file and parse the data
in it. The format of the file is:
8 bits number of data pakets
6 bits offset
20 bits time
8 states

How can I read this fields in binary? I see the struct module and the
binascii, but I can't split the diferent fiels.


You can use struct to create a long from your data and then work on that
with the usual bitwise operators for shift and boolean and so that you
extract the actual data. If the above is the entire spec, I see some
problems to get the alignment proper, but with a bit of shifting you should
be able to work around that.
--
Regards,

Diez B. Roggisch
Jul 18 '05 #2

P: n/a
Hello Derfel,
I have a file of binary data. I want to read this file and parse the data in
it. The format of the file is:
8 bits number of data pakets
6 bits offset
20 bits time
8 states

How can I read this fields in binary? I see the struct module and the
binascii, but I can't split the diferent fiels.

--- bitter.py ---
# Testing
'''
b = BitStream(chr(int("10111010", 2)))
b.get_bits(3) 5 b.get_bits(2) 3 b.get_bits(3) 2 b.get_bits(0) 0 b.get_bits(1)

Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/tmp/bitter.py", line 27, in get_bits
raise EOFError
EOFError
'''

from array import array

class BitStream:
'''Bit stream implementation'''
def __init__(self, data):
self.arr = array("c", data)
self.bit = 0 # Current output bit

def get_bits(self, count):
'''Get `count' bits from stream'''
if count == 0: # Nothing to get
return 0

if not self.arr: # EOF
raise EOFError

value = 0
for c in range(count):
if not self.arr: # EOF
raise EOFError
value <<= 1
shifted = ord(self.arr[0]) >> (8 - self.bit - 1)
value |= (shifted & 1)
self.bit += 1
if self.bit == 8: # Get new byte
self.arr.pop(0)
self.bit = 0

return value

def _test():
from doctest import testmod
import bitter

testmod(bitter)

if __name__ == "__main__":
_test()
--- bitter.py ---

HTH.
Miki
Jul 18 '05 #3

P: n/a
Miki Tebeka escribió en news:40**************@zoran.com:
Hello Derfel,

--- bitter.py ---


Thanks for the module, it works well and is the solution to my problem.
Thanks.
Regards
--
Derfel
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.