performing action on set of charecters

Discussion in 'Python' started by jeff, Jan 22, 2004.

  1. jeff

    jeff Guest

    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
     
    jeff, Jan 22, 2004
    #1
    1. Advertising

  2. 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
     
    Francis Avila, Jan 22, 2004
    #2
    1. Advertising

  3. jeff

    Paul Watson Guest

    "jeff" <> wrote in message
    news:...
    > 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
     
    Paul Watson, Jan 23, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Smith
    Replies:
    1
    Views:
    334
    Kevin Spencer
    Jun 17, 2005
  2. Nathan Sokalski

    Performing a download along with another action

    Nathan Sokalski, Nov 16, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    424
    Cor Ligthert [MVP]
    Nov 16, 2005
  3. Bruce Sam
    Replies:
    1
    Views:
    376
    =?UTF-8?b?TMSByrtpZSBUZWNoaWU=?=
    Dec 17, 2004
  4. 28tommy
    Replies:
    2
    Views:
    304
    28tommy
    Dec 28, 2005
  5. Serge Myrand

    Performing action before moving to another page

    Serge Myrand, Jul 12, 2004, in forum: ASP General
    Replies:
    1
    Views:
    121
    Bullschmidt
    Jul 13, 2004
Loading...

Share This Page