On 15/06/2006 9:24 AM, Wojciech Muła wrote:
> [EMAIL PROTECTED] wrote:
>> The problem is negative values. If the unit returns the hex value 'e7',
>> it means -25, but python says it's 231:
>> ---------------------------
>>>>> int('e7', 16)
>> 231
>> ---------------------------
>>
>> Does anyone have a clue a to what I need to do?
> 
> def u2(x):
>       if x & 0x80: # MSB set -> neg.
>               return -((~x & 0xff) + 1)

Holy obfuscation, Batman!

>       else:
>               return x
>               
>>>> u2(int('e7', 16))
> -25
>>>> u2(int('12', 16))
> 18
> 
> w.
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to