Long int to int

Paul Rubin phr-n2002a at nightsong.com
Mon Feb 11 07:05:37 EST 2002


Tim Howarth <tim at worthy.demon.co.uk> writes:
> >   a = eval('0x%x' % a)
> > 
> > might be a little more palatable.  
> 
> Nicer but I'm not sure that it works, I want
> 
> 0xFFFFFFFFL
> to become
> -1

That's the result I get in Python 2.2.  I don't have 2.1 on this system.
1.5.2 raises an exception when it tries to convert 0xFFFFFFFFL to hex.

> besides I grew up believeing eval==evil.

Yes, I agree.  However, the obvious int('FFFFFFFF',16) raises
ValueError because the literal is too large.  I guess you could use
binascii and struct to convert the hex string to a binary string and
then pack the binary string into an integer.  Ouch.







More information about the Python-list mailing list