Long int to int

Tim Howarth tim at worthy.demon.co.uk
Mon Feb 11 05:40:32 EST 2002


I need to convert some long int values to normal ints.
(4 byte words)

e.g. a=0x81234567L

(masked from a longer int e.g. 0xAA81234567L hence as a L)

b=int(a) works if a is less than 0x7ff..

If greater (i.e. 2's complement negative int) an error occurs.

I can convert by shifting bits, converting to int and shifting back.

a=int(a & 0xFFFF) | int(a>>16 & 0xFFFF)<<16

Is there not a neater, more correct way to go from

0x81234567L
to
0x81234567    ?


(Or worse, I can convert with eval

a = str(hex(a))[2:-1]
a = eval('0x'+a)

works but

int (str(hex(a))[2:-1],16) fails.)


-- 
___
 |im    ---- ARM Powered ----



More information about the Python-list mailing list