473,480 Members | 1,807 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

bitstreams

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

Hoping for an answer and wishing a happy day
LOBI

Jul 18 '05 #1
4 1722
Andreas Lobinger wrote:
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

Hoping for an answer and wishing a happy day
LOBI


You might start with something like the following as a primitive,
tweak it to deal correctly with your byte sex (big-endian vs. little
endian), bit numbering, and such. Then when that works, add a
current position and auto-advance stuff. Then you can decide if you
want to go in bigger "bytes" by something like 'L' instead of 'B'
below. Note that 1000 bits is not so very big that reading the file
into memory is a bad idea. In fact, I'd only think about that above
about a million bits.

import array

class Bitvector(object):
def __init__(self, filename):
datafile = file(filename, 'rb')
self.data = array.array('B', datafile.read())
datafile.close()
self.elbits = self.data.itemsize * 8 # 8 bits/byte
def grab(self, start, length):
word, part = divmod(start, self.elbits)
sofar = self.elbits - part
result = self.data[word] & ((1L << sofar) - 1)
# 1L above so no sign bit hassles below
while sofar < length:
word += 1
result = (result << self.elbits) | self.data[word]
sofar += self.elbits
return result >> sofar - length

--
-Scott David Daniels
Sc***********@Acm.Org
Jul 18 '05 #2
Andreas Lobinger <an**************@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
Jul 18 '05 #3
Aloha,

Michael Hudson wrote:
Andreas Lobinger <an**************@netsurf.de> writes:
can anyone here recommend a implementation for bitstreams?

For a project i need to extract bitfields from a file containing
one large bitstream (length >> 1000bit). The bitfields (1bit-24bit)
are not aligned to char boundaries (8bit).
Well, a thousand bits really isn't very many. Read the lot and slurp
it into a long?


Thanks to both replies by now. Two points. length>>1000bits means
that i'm talking about an implementation that runs up to 1000000bits.
In the moment i'm working with a string in memory and use a state
maschine to get to the first byte, use string.struct to get
the word/long containing all bits and a little bit masking.

There is no real problem writing code for this, but i thought that
it could be that there are already implementations for bitstreams
f.e. mpeg-reading, a zlib implementation or other coding/decoding
things...

Wishing a happy day
LOBI
Jul 18 '05 #4
Andreas Lobinger <an**************@netsurf.de> writes:
Aloha,

Michael Hudson wrote:
Andreas Lobinger <an**************@netsurf.de> writes:
can anyone here recommend a implementation for bitstreams?

For a project i need to extract bitfields from a file containing
one large bitstream (length >> 1000bit). The bitfields (1bit-24bit)
are not aligned to char boundaries (8bit).
Well, a thousand bits really isn't very many. Read the lot and slurp
it into a long?


Thanks to both replies by now. Two points. length>>1000bits means
that i'm talking about an implementation that runs up to 1000000bits.


Oh :-)
In the moment i'm working with a string in memory and use a state
maschine to get to the first byte, use string.struct to get
the word/long containing all bits and a little bit masking.
Right.
There is no real problem writing code for this, but i thought that
it could be that there are already implementations for bitstreams
f.e. mpeg-reading, a zlib implementation or other coding/decoding
things...


Not to my knowledge! Such things are generally done by C libraries
that are then wrapped for use by Python, in my experience.

Writing a C extension to do what you need probably isn't amazingly
hard (so long as you don't need bitfields wider than the machine word
size I guess).

Cheers,
mwh

--
8. A programming language is low level when its programs require
attention to the irrelevant.
-- Alan Perlis, http://www.cs.yale.edu/homes/perlis-alan/quotes.html
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2133
by: Luis P. Mendes | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 hi, I've already read many pages on this but I'm not able to separate the string 'R0\1.2646\1.2649\D' in four elements, using the \ as the...
8
17543
by: aling | last post by:
Given the bit field struct: int main() { union { struct { unsigned short s1 : 4; unsigned short s2 : 3;
10
2479
by: Kristian Nybo | last post by:
Hi, I'm writing a simple image file exporter as part of a school project, and I would like to write completely platform-independent code if at all possible. The problem I've run into is that...
32
4162
by: bluejack | last post by:
Ahoy: For as long as I've been using C, I've vacillated on the optimal degree of encapsulation in my designs. At a minimum, I aggregate data and code that operate on that data into classlike...
0
7049
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6912
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7052
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7092
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
4790
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
2989
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1304
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
565
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
188
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.