trouble converting c++ bitshift to python equivalent

Discussion in 'Python' started by nanodust@gmail.com, May 24, 2007.

  1. Guest

    hello all

    i am relatively new to python, catching on, but getting stuck on
    simple thing:

    i have two string bytes i need to push into a single (short) int, like
    so in c:

    temp = strBuf[2];

    temp = (temp<<7)+(strBuf[1]);

    c code works, but having trouble getting python to perform same
    function!

    keep getting type & operator errors (i apparently can't bitshift on
    str or int?)

    curious what the best way is to do this, in python...

    i'll stick w/ it & post when i sort it


    meanwhile, any help greatly appreciated
    , May 24, 2007
    #1
    1. Advertising

  2. On 2007-05-24, <> wrote:
    > hello all
    >
    > i am relatively new to python, catching on, but getting stuck on
    > simple thing:
    >
    > i have two string bytes i need to push into a single (short) int, like
    > so in c:
    >
    > temp = strBuf[2];
    >
    > temp = (temp<<7)+(strBuf[1]);


    (ord(strBuf[2])<<7) + ord(strBuf[1])

    --
    Grant Edwards grante Yow! This PORCUPINE knows
    at his ZIPCODE ... And he has
    visi.com "VISA"!!
    Grant Edwards, May 24, 2007
    #2
    1. Advertising

  3. Steve Holden Guest

    wrote:
    > hello all
    >
    > i am relatively new to python, catching on, but getting stuck on
    > simple thing:
    >
    > i have two string bytes i need to push into a single (short) int, like
    > so in c:
    >
    > temp = strBuf[2];
    >
    > temp = (temp<<7)+(strBuf[1]);
    >
    > c code works, but having trouble getting python to perform same
    > function!
    >
    > keep getting type & operator errors (i apparently can't bitshift on
    > str or int?)
    >
    > curious what the best way is to do this, in python...
    >
    > i'll stick w/ it & post when i sort it
    >
    >
    > meanwhile, any help greatly appreciated
    >

    You should really use the struct module for that type of conversion, but
    you also need to know that indexing of lists and tuples starts at 0, not 1.

    For the specific case that you're discussing, you could convert each
    character to its corresponding integer value and use shifting and adding
    with those:

    temp = (ord(strBuf[1]) << 8) + ord(strBuf[0])

    Obviously if the byte order is wrong you would need to reverse the 0 and
    1 elements.

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://del.icio.us/steve.holden
    ------------------ Asciimercial ---------------------
    Get on the web: Blog, lens and tag your way to fame!!
    holdenweb.blogspot.com squidoo.com/pythonology
    tagged items: del.icio.us/steve.holden/python
    All these services currently offer free registration!
    -------------- Thank You for Reading ----------------
    Steve Holden, May 24, 2007
    #3
  4. Guest

    > (ord(strBuf[2])<<7) + ord(strBuf[1])


    wow, thank you - works perfectly !

    and much more elegant than what i was up to.

    thanks again...
    , May 24, 2007
    #4
  5. Guest

    > You should really use the struct module for that type of conversion, but
    > you also need to know that indexing of lists and tuples starts at 0, not 1.


    indeed, i used to have temp = unpack('h', tBuf[1,3])

    but it was a hack (and as such a bit off ;) as i was having troubles
    casting

    not quite used to python yet, will get there...


    thanks again !!
    , May 24, 2007
    #5
  6. On 2007-05-24, Steve Holden <> wrote:

    >> i have two string bytes i need to push into a single (short) int, like
    >> so in c:
    >>
    >> temp = strBuf[2];
    >>
    >> temp = (temp<<7)+(strBuf[1]);


    > You should really use the struct module for that type of conversion,


    The struct module doesn't know how to deal with the OP's case
    where only 7 bits are used from each byte. OTOH, if the 7 was
    a typo and he really wanted to shift by 8 bits, then struct is
    an option.

    > but you also need to know that indexing of lists and tuples
    > starts at 0, not 1.


    Ah yes. I wondered about that also, but I assumed what he
    acutally had was a two-byte field in a longer string.

    --
    Grant Edwards grante Yow! Jesuit priests are
    at DATING CAREER DIPLOMATS!!
    visi.com
    Grant Edwards, May 24, 2007
    #6
  7. On 2007-05-24, <> wrote:
    >> You should really use the struct module for that type of conversion, but
    >> you also need to know that indexing of lists and tuples starts at 0, not 1.

    >
    > indeed, i used to have temp = unpack('h', tBuf[1,3])


    Which probably should have been

    temp = unpack('h', tBuf[1:3])[0]

    But that still doesn't do the same thing as your example which
    only used 7 bits from each byte.

    > but it was a hack (and as such a bit off ;) as i was having
    > troubles casting


    Python doesn't have "casting".

    --
    Grant Edwards grante Yow! A can of ASPARAGUS,
    at 73 pigeons, some LIVE ammo,
    visi.com and a FROZEN DAQUIRI!!
    Grant Edwards, May 24, 2007
    #7
    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. Ryan
    Replies:
    4
    Views:
    2,968
    Rusty Wright
    Feb 14, 2005
  2. bollod
    Replies:
    12
    Views:
    508
    Richard Heathfield
    Dec 27, 2003
  3. Replies:
    8
    Views:
    330
    Larry I Smith
    Feb 10, 2006
  4. Serve Laurijssen

    bitshift operator problem

    Serve Laurijssen, Sep 5, 2008, in forum: C Programming
    Replies:
    13
    Views:
    610
    Ben Bacarisse
    Sep 6, 2008
  5. Bill Cunningham

    bitshift

    Bill Cunningham, Nov 8, 2011, in forum: C Programming
    Replies:
    1
    Views:
    291
    Lew Pitcher
    Nov 9, 2011
Loading...

Share This Page