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

cino hilliard hillcino368@hotmail.com
Wed Jun 18 19:43:01 2003


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. In other words, you can only go from base 2 to 36 to decimal 
base 10 only. Looking through rose colored glasses I thought I had found an 
arbitrary precision base to base converter as I had built in Basic but only 
good to double precision or 52 place
precision in zbasic. I have converted this routine in python to take 
advantage of the arbitrary integer
precision.

Copy, paste, import and try the example in the comments. Maybe the script 
should be symplified more
to python syntax and be included in python release xxx. The long(a,radix) 
int(a,radix) only converts a
string from a base 2 to 36 to base 10. The module below overcomes this 
limitation and then some.

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 ?

#Module convert.py      def base()    convert bases  from 2 to 62 char 0-9, 
A-Z,a-z  format
#                                        By Cino Hilliard
# usage: >>> import convert
#           >>> convert.base(base from,base to,'value string')
# convert.base(10,62,"1956790901451511461242225989521377674044432983494703")

def base(r1,r2,num):              #convert the string in base r1 to a string 
in base r2
    import math                     #we need this to get the power of
    num = str(num)
    dec = 0                       #Clear to allow loop input per base
    ln  = len(num)                #save length of string to convert for 
later parsing
    j=0
    while j < ln:                 #parse the input string
        asci = ord(num[j])        #Get the ascii code of the char in string 
to convert
        temp   = r1**(ln-j-1)     #Compute the powers of the radix to 
convert from
        ascii2 = decimal(asci)    #Get the decimal value of the ascii code
        dec += ascii2*temp        #Multiply by decimal value and power of 
radix
        j+=1
    RDX = ""
    PWR = math.log(dec)/math.log(r2)     #Get max power of r2 the radix to 
convert to
    j = int(PWR)
    while j >= 0:                        #Divide by descending powers of the 
radix to
        Q   = dec/(r2**j)                #get the ascii values
        dec = dec%(r2**j)                #get the remainder of divison for 
next division
        tmp = chr(ascii(Q))
        RDX = RDX + tmp                  #Concatenate the output string
        j-=1
    print RDX                            #Return the converted r1 to r2 
radix string
def ascii(Q1):
    if   Q1 < 10:                        # 0-9 = ascii 48 - 57
         Q1+= 48
    else:
         if Q1 > 9 and Q1 < 36:          # A-Z = ascii 65 - 90
              Q1+= 55
         else:
              Q1+= 61                    # a-z = ascii 97 - 122
    return Q1
def decimal(Q1):                         # 0 - 9 < ascii 58
    if   Q1 < 58:
         Q1-= 48
    else:
         if Q1 > 57 and Q1 < 97:         # A - Z < ascii 97
              Q1-= 55
         else:
              Q1-= 61                    # a - z = ascii 97 - 122
    return Q1
    import math
    num = str(num)
    dec = 0                       #Clear to allow loop input per base
    ln  = len(num)                #save length of string to convert for 
later parsing
    j=0
    while j < ln:                 #parse the input string
        asci = ord(num[j])        #Get the ascii code of the char in string 
to convert
        temp   = r1**(ln-j-1)     #Compute the powers of the radix to 
convert
        ascii2 = decimal(asci)    #Get the decimal value of the ascii code
        dec += ascii2*temp        #Multiply by decimal value and power of 
radix
        j+=1
    RDX = ""
    PWR = math.log(dec)/math.log(r2)     #Get max power of r2 the radix to 
convert to
    j = int(PWR)
    while j >= 0:                        #Divide by descending powers of the 
radix to
        Q   = dec/(r2**j)                #get the ascii values
        dec = dec%(r2**j)                #get the remainder of divison for 
next division
        tmp = chr(ascii(Q))
        RDX = RDX + tmp                  #Concatenate the output string
        j-=1
    print RDX                            #Return the converted r1 to r2 
radix string
def ascii(Q1):
    if   Q1 < 10:                        # 0-9 = ascii 48 - 57
         Q1+= 48
    else:
         if Q1 > 9 and Q1 < 36:          # A-Z = ascii 65 - 90
              Q1+= 55
         else:
              Q1+= 61                    # a-z = ascii 97 - 122
    return Q1
def decimal(Q1):                         # 0 - 9 < ascii 58
    if   Q1 < 58:
         Q1-= 48
    else:
         if Q1 > 57 and Q1 < 97:         # A - Z < ascii 97
              Q1-= 55
         else:
              Q1-= 61                    # a - z = ascii 97 - 122
    return Q1

>>>convert.base(10,62,"3996168503701201585343125040165201358016538092484388"
PythonIsAVeryPowerfulLanguage

>>>convert.base(62,10,"11JJEO8uplRL22r3MHTOCMqG6JAoq13eZ63xNsSGcA")

Have fun in the facinating world of numbers.

662042196960720285629 base 62

_________________________________________________________________
Tired of spam? Get advanced junk mail protection with MSN 8. 
http://join.msn.com/?page=features/junkmail