473,385 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Write bits in file

Hi

I have a specific format and I need binary representation. Does
Python have some built-in function which will, for instance, represent
number 15 in exactly 10 bits?
Jun 27 '08 #1
12 5995
You want your file considered as a sequence of bits rather
than a sequence of 8-bit bytes, do you? is the 10-bit
bit-pattern to be stored at an arbitrary bit-position in
the file, or is the whole file regularly subdivided
at 10-bit intervals?
Monica Leko wrote:
Hi

I have a specific format and I need binary representation. Does
Python have some built-in function which will, for instance, represent
number 15 in exactly 10 bits?
Jun 27 '08 #2
On May 18, 2:20*pm, Ken Starks <stra...@lampsacos.demon.co.ukwrote:
You want your file considered as a sequence of bits rather
than a sequence of 8-bit bytes, do you?
Yes.
is the 10-bit
bit-pattern to be stored at an arbitrary bit-position in
the file
Yes. I need arbitrary, 8bits, than 10 bits for something else, than
sequence of bytes, than 10 bits again, etc.
Jun 27 '08 #3
En Sun, 18 May 2008 10:36:28 -0300, Monica Leko <mo*********@gmail.comescribió:
On May 18, 2:20*pm, Ken Starks <stra...@lampsacos.demon.co.ukwrote:
>You want your file considered as a sequence of bits rather
than a sequence of 8-bit bytes, do you?

Yes.
>is the 10-bit
bit-pattern to be stored at an arbitrary bit-position in
the file

Yes. I need arbitrary, 8bits, than 10 bits for something else, than
sequence of bytes, than 10 bits again, etc.
If you really need arbitrary bit sequences that aren't synchonized into bytes, I think there is a BitVector o BitArray package somewhere.
But if you mostly have bytes and sparsely a different size, I think the struct module and some gymnastics involving bitwise operations would be enough.

--
Gabriel Genellina

Jun 27 '08 #4
I admit that I was mostly just interested in getting your
question clarified, rather than having any great experise.

But a bit of Googling took me to the 'Bit vector' module,
[I googled: 'python ("bit array" OR "bit vector")']
which might be what you are after. I have no experience
with it, myself:

http://cobweb.ecn.purdue.edu/~kak/di...tor-1.4.1.html

Monica Leko wrote:
On May 18, 2:20 pm, Ken Starks <stra...@lampsacos.demon.co.ukwrote:
>You want your file considered as a sequence of bits rather
than a sequence of 8-bit bytes, do you?

Yes.
>is the 10-bit
bit-pattern to be stored at an arbitrary bit-position in
the file

Yes. I need arbitrary, 8bits, than 10 bits for something else, than
sequence of bytes, than 10 bits again, etc.

Jun 27 '08 #5
Monica Leko wrote:
Hi

I have a specific format and I need binary representation. Does
Python have some built-in function which will, for instance, represent
number 15 in exactly 10 bits?
The "struct" module will let you format Python data as a binary
object of specified format. But it doesn't support arbitrary bit-width
fields; integers can be 1, 2, 4, or 8 bytes.

If you have a space problem, you might use the "struct" module to
convert to a reasonably concise binary representation, then use the
"gzip" module to compress the file down further.

John Nagle
Jun 27 '08 #6
Monica Leko <mo*********@gmail.comwrote:
>
I have a specific format and I need binary representation. Does
Python have some built-in function which will, for instance, represent
number 15 in exactly 10 bits?
For the record, I'd like to point out that even C cannot do this. You need
to use shifting and masking to produce a stream of 8-bit bytes, or to
extract your values from a stream of 8-bit bytes.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jun 27 '08 #7
On Sun, 18 May 2008 06:36:28 -0700 (PDT)
Monica Leko <mo*********@gmail.comwrote:
Yes. I need arbitrary, 8bits, than 10 bits for something else, than
sequence of bytes, than 10 bits again, etc.
Here's something to get you started. No guarantees, but I managed to
write four 10 bit numbers to a file producing 5 bytes, saying hello. I
was just looking for an excuse to use send.

P.

def gen(f):
L = []

def flush(L):
L.reverse()
s = ''.join(map(str,L))
j = int(s,2)
f.write(chr(j))

while 1:
x = yield
if x in [0,1]:
L.append(x)
else:
break
if len(L) == 8:
flush(L)
L = []
if L:
while len(L) < 8:
L.append(0)
flush(L)
yield

def tenbits(i):
for j in range(9,-1,-1):
yield i >j & 1

def charbits(s):
for c in s:
i = ord(c)
for j in range(8):
yield i >j &1

def byten(L):
while L:
yield L[:10]
L = L[10:]

def test():
f = file('out.dat','w')
g = gen(f)
g.send(None)
for x in [90,611,397,758]:
for bit in tenbits(x):
g.send(bit)
g.send('stop')
f.close()

def test1():
bits = list(charbits('hello'))
L = []
for x in byten(bits):
L.append(int(''.join(map(str,x)),2))
print L

if __name__=='__main__':
test()
Jun 27 '08 #8
Monica Leko <mo*********@gmail.comwrote:
On May 18, 2:20?pm, Ken Starks <stra...@lampsacos.demon.co.ukwrote:
You want your file considered as a sequence of bits rather
than a sequence of 8-bit bytes, do you?

Yes.
is the 10-bit bit-pattern to be stored at an arbitrary
bit-position in the file

Yes. I need arbitrary, 8bits, than 10 bits for something else, than
sequence of bytes, than 10 bits again, etc.
You could try

http://construct.wikispaces.com/

which could well do exactly what you want.

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Jun 27 '08 #9
On May 20, 10:30*am, Nick Craig-Wood <n...@craig-wood.comwrote:
Monica Leko <monica.l...@gmail.comwrote:
*On May 18, 2:20?pm, Ken Starks <stra...@lampsacos.demon.co.ukwrote:
You want your file considered as a sequence of bits rather
than a sequence of 8-bit bytes, do you?
*Yes.
is the 10-bit bit-pattern to be stored at an arbitrary
bit-position in the file
*Yes. *I need arbitrary, 8bits, than 10 bits for something else, than
*sequence of bytes, than 10 bits again, etc.

You could try

*http://construct.wikispaces.com/

which could well do exactly what you want.

--
Nick Craig-Wood <n...@craig-wood.com--http://www.craig-wood.com/nick
Do you have space to waste? If so, how much? Worst case, you could
be looking at a factor of 32 or higher, if each bit took an entire
system word.
Jun 27 '08 #10
On May 20, 12:14 am, Tim Roberts <t...@probo.comwrote:
Monica Leko <monica.l...@gmail.comwrote:
I have a specific format and I need binary representation. Does
Python have some built-in function which will, for instance, represent
number 15 in exactly 10 bits?

For the record, I'd like to point out that even C cannot do this. You need
to use shifting and masking to produce a stream of 8-bit bytes, or to
extract your values from a stream of 8-bit bytes.
Technically specifying 8-bits isn't quite accurate, as C allows for 9-
bit bytes and other variations depending on the architecture. But
that may be overly pedantic unless you have a PDP-10 laying around
that you're writing C code on or something like that.
Jun 27 '08 #11
"sj*******@yahoo.com" <sj*******@yahoo.comwrote:
>On May 20, 12:14 am, Tim Roberts <t...@probo.comwrote:
>Monica Leko <monica.l...@gmail.comwrote:
>I have a specific format and I need binary representation. Does
Python have some built-in function which will, for instance, represent
number 15 in exactly 10 bits?

For the record, I'd like to point out that even C cannot do this. You need
to use shifting and masking to produce a stream of 8-bit bytes, or to
extract your values from a stream of 8-bit bytes.

Technically specifying 8-bits isn't quite accurate, as C allows for 9-
bit bytes and other variations depending on the architecture. But
that may be overly pedantic unless you have a PDP-10 laying around
that you're writing C code on or something like that.
As long as we are being pedantic, and I don't mind that, I would point out
that I didn't actually say that C worked in 8-bit bytes. I was very
careful to say merely that, assuming you wanted a stream of 8-bit bytes,
you need to use shifting and masking to produce it.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jun 27 '08 #12
Tim Roberts wrote:
Monica Leko <mo*********@gmail.comwrote:
>I have a specific format and I need binary representation. Does
Python have some built-in function which will, for instance, represent
number 15 in exactly 10 bits?

For the record, I'd like to point out that even C cannot do this. You need
to use shifting and masking to produce a stream of 8-bit bytes, or to
extract your values from a stream of 8-bit bytes.

Hmmmm, bitfields are exactly non-aligned bits in a structure and are
part of ANSI C. Although C gives no guarantee of the ordering of fields
within machine words ... so bitfieds are of limited use in portable
programs unless they are 0-initialized.

IIRC, Huffman code uses arbitrary length bit strings and is the basis of
many compression algorithms.


Jun 27 '08 #13

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

Similar topics

8
by: Sowen | last post by:
Hi, all I am wondering how to write bits by using ofstream? I have finished a huffman tree, but how can I write the bits to the file in order to gain compression? for example, 'A' returns a...
10
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...
9
by: John | last post by:
Hi, I need to write out bits that I receive from another process. These are boolean values. I need there to be 8 bits in every byte. I know I could write these bits out as char's using one bit...
12
by: Christian Blackburn | last post by:
Hi Gang, I would like to know how to write a single bit to a file (if that's possible). I would like to repetitiously write a 1 or 0 as the data I'm trying to save would dictate. Thanks in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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
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
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
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...

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.