performing action on set of charecters

Francis Avila francisgavila at yahoo.com
Thu Jan 22 15:24:24 EST 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




More information about the Python-list mailing list