443,908 Members | 1,860 Online
Need help? Post your question and get tips & solutions from a community of 443,908 IT Pros & Developers. It's quick & easy.

# trouble converting c++ bitshift to python equivalent

 P: n/a 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 '07 #1
6 Replies

 P: n/a On 2007-05-24, na******@gmail.com

 P: n/a na******@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)+(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 ---------------- May 24 '07 #3

 P: n/a (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 '07 #4

 P: n/a 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 '07 #5

 P: n/a On 2007-05-24, Steve Holden i have two string bytes i need to push into a single (short) int, likeso 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 May 24 '07 #6

 P: n/a On 2007-05-24, na******@gmail.com You should really use the struct module for that type of conversion, butyou 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!! May 24 '07 #7

### This discussion thread is closed

Replies have been disabled for this discussion.