473,385 Members | 2,005 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.

performing action on set of charecters

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
Jul 18 '05 #1
2 1393
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

Jul 18 '05 #2

"jeff" <gr**********@hotmail.com> wrote in message
news:cd**************************@posting.google.c 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
Jul 18 '05 #3

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

Similar topics

5
by: Desmond | last post by:
Can anyone tell me ho to get certain charecters to be displayed. When sending email with (') i get (\) instead. i.e. This dosen't work becomes dosen\t Is there some kind of escape key to get...
3
by: special_agent69 | last post by:
Not sure if this is a stoopid question or not. If it is, my apologies. I'd like to have an ASP form performing a relatively simple calculation. For example, if I wanted to create a simple...
1
by: Alex Smith | last post by:
Hi friends, I want to read all charecters of pdf file using char array. Thanks, Alex.
0
by: Tamra | last post by:
Pages run perfectly on any computer with the .Net framework installed. Not so on those that do not. For example. One page uses a sqlDataSourceControl to populate a dropDownList. When an item is...
1
by: Alex Smith | last post by:
Hi friends, I want to read all charecters of pdf file using char array. Thanks, Alex.
3
by: Sameh Ahmed | last post by:
hello there need to create an application that checks for a condition every 5 minutes and performs a certain action depending on whether the condition was met or not. what is the best way to do...
2
by: 28tommy | last post by:
Hello all, I know how to exclude 1 charecter from a compiled sequence of the re module- '', but if I want to exclude a word or a sequence as one unit (not as separate charecters) to be...
0
by: Vishal Dalsania | last post by:
Hello all, I have created an application in ASP.NET 2.0. When i run it from internet explorer the aspx page shows some garbage charecters in it. I tried looking in to the HTML source of the page...
9
by: monomaniac21 | last post by:
hi all i want to use hyperlinks to 'load' content by changing the display of div tags. the problem i have is that unless i specify a href the anchor does not change the mouse pointer on hover...
2
by: nityagopikrishnan | last post by:
how do we write a programme to find the frq of charecters of a string eg you are great output no of y=1 no of o=1 no of u=1 no of a=2 no of r=2 no of e =2 no of g=1
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:
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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.