[Tutor] built in functions int(),long()

Jeff Shannon jeff@ccvcorp.com
Thu Jun 19 14:39:02 2003


cino hilliard wrote:

> Hi Greg et al,
> Thanks to all for your replies.
>
> Greg,
> Yes your analysis  it makes sense but not according to the definitions 
> at least as I understand them.
>
> atoi( s[, base]) is not clear and threw me off. Also The radix 
> parameter gives the base for the conversion and may be any integer in 
> the range [2, 36]
> should read The radix parameter gives the base for the string you want 
> to convert to python integer. The Radix  may be any integer  2 to 36. 
> An example would have clarified it.
>
> "Convert string s to an integer in the given base." I interpreted as 
> convert ("123",2) to 111011 base 2. What it really means is convert 
> the string base 2 to decimal.


No, because an integer doesn't have a base associated with it, only the 
representation does.

How many asterisks is this?    *  *  *  *  *  *  *  *  *  *  *  *  *

"13" in base 10
"15" in base 8
"D" in base 16
"11" in base 12
"31" in base 4
"1101" in base 2
"XIII" in Roman ;)

I use quotes because these are symbols (strings) representing something 
else (a number) -- but they all represent the *same* number.  A "pure" 
number doesn't have a base associated with it -- the base describes the 
representation, not the number itself.  The int() function (and atoi(), 
which is really the same thing) will convert a representation (which has 
a base associated with it) to the underlying number (which has no base).  

What has you confused is that Python, when asked to show you an integer, 
will automatically convert that integer into base 10 for display. 
 However, that's a feature of the display, not of the integer itself -- 
it just so happens that for humans, base 10 is easiest to read, so 
Python accommodates us (unless we specifically request otherwise, 
through the use of hex() or similar functions).  Of course, internally 
the computer will store all integers in binary format (base 2), because 
that's the way that computers are wired.

> Question. How can I get convert.base(r1,r2,str) without having to type 
> the quotes for the string?
> Eg., convert(16,10,FFFF) instead of convert(16,10,"FFFF") = 65535 ? 


You can't, because the Python parser would have way of knowing what's 
intended -- is FFFF a string? A hexidecimally-represented integer? A 
variable?  As a matter of fact, the last is exactly how the parser sees 
this -- it looks at FFFF as a variable name, and attempts to look up a 
value for it (probably resulting in a NameError).  You could, in this 
case, specifically designate that it's a hexidecimal number by typing it 
as 0xFFFF -- but then, a function call is unnecessary.  In order for 
Python to see it as a string, which is necessary for the general case of 
converting some arbitrary representation, you need to have quotes.

(I'll refrain from commenting on your code, but it looks very confused 
-- you seem to have both decimal() and ascii() defined twice, among 
other things...)

Jeff Shannon
Technician/Programmer
Credit International