string.atoi and string.atol broken?

Dennis Benzinger Dennis.Benzinger at gmx.net
Tue Jan 25 18:42:40 EST 2005


Mike Moum wrote:
> I think there may be a bug in string.atoi and string.atol.  Here's some 
> output from idle.
> 
> 
>>Python 2.3.4 (#2, Jan  5 2005, 08:24:51) 
>>[GCC 3.3.5 (Debian 1:3.3.5-5)] on linux2
>>Type "copyright", "credits" or "license()" for more information.
>>
>>    ****************************************************************
>>    Personal firewall software may warn about the connection IDLE
>>    makes to its subprocess using this computer's internal loopback
>>    interface.  This connection is not visible on any external
>>    interface and no data is sent to or received from the Internet.
>>    ****************************************************************
>>    
>>IDLE 1.0.4      
>>
>>>>>import string as s
>>>>>s.atoi('2',3)
>>
>>2
>>
>>>>>s.atoi('4',3)
>>
>>Traceback (most recent call last):
>>  File "<pyshell#2>", line 1, in -toplevel-
>>    s.atoi('4',3)
>>  File "/usr/lib/python2.3/string.py", line 220, in atoi
>>    return _int(s, base)
>>ValueError: invalid literal for int(): 4
>>
>>>>>s.atoi('12',11)
>>
>>13
>>
>>>>>s.atoi('13',4)
>>
>>7
>>
>>>>>s.atoi('12',4)
>>
>>6
>>
>>>>>s.atoi('8',4)
>>
>>Traceback (most recent call last):
>>  File "<pyshell#6>", line 1, in -toplevel-
>>    s.atoi('8',4)
>>  File "/usr/lib/python2.3/string.py", line 220, in atoi
>>    return _int(s, base)
>>ValueError: invalid literal for int(): 8
>>
> 
> s.atoi('4',3) should result in 11
> 
> s.atoi('13',4) should result in 31
> 
> s.atoi('12',4) should result in 30
> 
> s.atoi('8',4) is legitimate, but it generates an error.
> 
> Is this a bug, or am I missing something obvious?
> [...]

That's not a bug, you'r missing something obvious.

The second parameter of string.atoi (or the int builtin)
is the base (or radix) in which the number you want to
convert is given.

For example string.atoi("777", 8) results in 511,
because 7 * 8**2 + 7 * 8**1 + 7 * 8**0 = 511.

Just out of curiosty:
What did you think what atoi does?
I don't understand how you came to expect that atoi('4',3)
should result in 11.


Bye,
Dennis



More information about the Python-list mailing list