bitstreams
Michael Hudson
mwh at python.net
Fri Jul 9 07:44:51 EDT 2004
Andreas Lobinger <andreas.lobinger at netsurf.de> writes:
> Aloha,
> can anyone here recommend a implementation for bitstreams?
>
> For a project i need to extract bitfields from a file containing
> one large bitstream (lenght >> 1000bit). The bitfields (1bit-24bit)
> are not aligned to char boundaries (8bit).
>
> Something like
> b1 = bitstream.fromfile('m1.bin')
> v1 = b1.getbits(startpos=3,length=10) # v ist a reg. int
> v2 = b1.getnext(8) # bits 0-7
> v3 = b1.getnext(10) # bits 8-17
Well, a thousand bits really isn't very many. Read the lot and slurp
it into a long? Hmm, not sure of an easy way to do that... something
like
a = array.array('B')
a.fromstring(open('m1.bin').read())
v = 0L
m = 1L
for b in a:
v += m*b
m *= 256
then use shifts & masks as desired on v.
There ought to be a cuter way to go from m1.bin to v, though...
Cheers,
mwh
--
There's a difference between random people with stripy jumpers,
and a respected scientist with a reputation.
-- Steve Kitson, ucam.chat
More information about the Python-list
mailing list