trouble converting c++ bitshift to python equivalent

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nanodust@gmail.com

    #1

    trouble converting c++ bitshift to python equivalent

    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)+(strB uf[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

  • Grant Edwards

    #2
    Re: trouble converting c++ bitshift to python equivalent

    On 2007-05-24, nanodust@gmail. com <nanodust@gmail .comwrote:
    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)+(strB uf[1]);
    (ord(strBuf[2])<<7) + ord(strBuf[1])

    --
    Grant Edwards grante Yow! This PORCUPINE knows
    at his ZIPCODE ... And he has
    visi.com "VISA"!!

    Comment

    • Steve Holden

      #3
      Re: trouble converting c++ bitshift to python equivalent

      nanodust@gmail. com 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)+(strB uf[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.blogs pot.com squidoo.com/pythonology
      tagged items: del.icio.us/steve.holden/python
      All these services currently offer free registration!
      -------------- Thank You for Reading ----------------

      Comment

      • nanodust@gmail.com

        #4
        Re: trouble converting c++ bitshift to python equivalent

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


        wow, thank you - works perfectly !

        and much more elegant than what i was up to.

        thanks again...

        Comment

        • nanodust@gmail.com

          #5
          Re: trouble converting c++ bitshift to python equivalent

          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 !!

          Comment

          • Grant Edwards

            #6
            Re: trouble converting c++ bitshift to python equivalent

            On 2007-05-24, Steve Holden <steve@holdenwe b.comwrote:
            >i have two string bytes i need to push into a single (short) int, like
            >so in c:
            >>
            > temp = strBuf[2];
            >>
            > temp = (temp<<7)+(strB uf[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

            Comment

            • Grant Edwards

              #7
              Re: trouble converting c++ bitshift to python equivalent

              On 2007-05-24, nanodust@gmail. com <nanodust@gmail .comwrote:
              >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!!

              Comment

              Working...