trouble converting c++ bitshift to python equivalent

N

nanodust

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
 
G

Grant Edwards

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])
 
S

Steve Holden

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

nanodust

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


wow, thank you - works perfectly !

and much more elegant than what i was up to.

thanks again...
 
N

nanodust

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

Grant Edwards

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.
 
G

Grant Edwards

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".
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top