trouble converting c++ bitshift to python equivalent

Grant Edwards grante at visi.com
Thu May 24 16:59:25 EDT 2007


On 2007-05-24, Steve Holden <steve at holdenweb.com> 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            



More information about the Python-list mailing list