Negative hex to int

Wojciech Muła wojciech_mula at poczta.null.onet.pl.invalid
Wed Jun 14 19:24:41 EDT 2006


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)
	else:
		return x
		
>>> u2(int('e7', 16))
-25
>>> u2(int('12', 16))
18
>>>

w.



More information about the Python-list mailing list