Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > performing action on set of charecters

Reply
Thread Tools

performing action on set of charecters

 
 
jeff
Guest
Posts: n/a
 
      01-22-2004
hiya,

Ive a load of binary in a file. Its 3 bit (2^3) and i wanna convert it
to an integer.

ive tried using theintergar = string.atoi(thebinary, 2), but that
doesnt take it as 3 bit binary

it has no spaces it it, so im a bit stuck as to how to do this with
python,

cheers

greg
 
Reply With Quote
 
 
 
 
Francis Avila
Guest
Posts: n/a
 
      01-22-2004
jeff wrote in message ...
>hiya,
>
>Ive a load of binary in a file. Its 3 bit (2^3) and i wanna convert it
>to an integer.
>
>ive tried using theintergar = string.atoi(thebinary, 2), but that
>doesnt take it as 3 bit binary
>
>it has no spaces it it, so im a bit stuck as to how to do this with
>python,
>
>cheers
>
>greg


Three bit binary?! Whoever designed that format is a sadomasochist.

You're going to have to separate out the bits in the byte stream (so that
each bit is a byte--use strings), take three-bit groupings, and multiply to
convert to integer.

If this is a single, isolated three-bit integer, your approach will involve
shifting. If it's a continuous stream, without any padding, you can work in
groups of 24 bits (4 bytes), which gives you 8 3-bit ints.

There are many different approaches, but all will be painful. I don't think
it would be much easier in C, either (bit manipulation is often a little
easier in C compared to Python, albeit more dangerous).

Search this group. Not too long ago there was talk of a module for working
with binary which you might find helpful.
--
Francis Avila

 
Reply With Quote
 
 
 
 
Paul Watson
Guest
Posts: n/a
 
      01-23-2004

"jeff" <> wrote in message
news: om...
> hiya,
>
> Ive a load of binary in a file. Its 3 bit (2^3) and i wanna convert it
> to an integer.
>
> ive tried using theintergar = string.atoi(thebinary, 2), but that
> doesnt take it as 3 bit binary
>
> it has no spaces it it, so im a bit stuck as to how to do this with
> python,
>
> cheers
>
> greg


Will the following do what you want?

#! /usr/bin/env python

f = file('bits.dat', 'rb')
a = f.read()
f.close()

vals = []
for b in enumerate(a):
v = ord(b[1])
if (b[0] % 3) == 0:
vals.append((v >> 5) & 0x07)
vals.append((v >> 2) & 0x07)
carryover = (v << 1) & 0x07
if (b[0] % 3) == 1:
vals.append(carryover | ((v >> 7) & 0x01))
vals.append((v >> 4) & 0x07)
vals.append((v >> 1) & 0x07)
carryover = (v << 2) & 0x04
if (b[0] % 3) == 2:
vals.append(carryover | ((v >> 6) & 0x07))
vals.append((v >> 3) & 0x07)
vals.append(v & 0x07)

print vals


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I want to exclude an expression of a sequence of charecters with regex 28tommy Python 2 12-28-2005 12:45 PM
Performing a download along with another action Nathan Sokalski ASP .Net 3 11-16-2005 08:21 AM
I want to read all charecters of pdf file ... Alex Smith ASP .Net 1 06-17-2005 01:25 PM
What charecters the lightweight component have? Bruce Sam Java 1 12-17-2004 08:22 AM
Performing action before moving to another page Serge Myrand ASP General 1 07-13-2004 10:13 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57