Negative hex to int
John Machin
sjmachin at lexicon.net
Wed Jun 14 19:27:54 EDT 2006
On 15/06/2006 9:24 AM, Wojciech Muła wrote:
> andreas.lydersen at gmail.com 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.
More information about the Python-list
mailing list