FAQ
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

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 2 of 3 | next ›
Discussion Overview
grouppython-list @
categoriespython
postedJan 22, '04 at 3:51p
activeJan 23, '04 at 7:46a
posts3
users3
websitepython.org

People

Translate

site design / logo © 2022 Grokbase