string.atoi() doesn't do negative base16 - why??
Steven Adams
adams_s at lab.eng.usyd.edu.au
Mon Jun 12 05:59:17 EDT 2000
Hi,
my quick hack for going back and forth between binary and base 10, 16 is
backfiring a bit..
I can form the values as a string, BUT, string.atoi() can't convert negative
base hexadecimal strings to integers.
does that make sense?
e.g
>>> import string
>>> hex(-20)
'0xffffffec'
>>> string.atoi('0xffffffec',16)
Traceback (innermost last):
File "<pyshell#28>", line 1, in ?
string.atoi('0xffffffec',16)
ValueError: atoi() literal too large: 0xffffffec
why is this?
because after looking at it for too long - I found exec() would do the trick
e.g.
>>> myHexString='0xffffffec'
>>> exec('l='+myHexString)
why does this work, but the conversion by string.atoi() not?? curiouser and
curiouser
confused,
Steven
More information about the Python-list
mailing list