#!/usr/bin/env python3.7 # Converts base-A to base-B number systems, # where A and B are between 2 and 36. # This code will only convert whole numbers. # get inputs print("") print("2 <= base <= 36") A = int(input(" base initial = ")) if A<2 or A>36: print("input error") quit() B = int(input(" base final = ")) if B<2 or B>36: print("input error") quit() print("using 0,1,...,9,A,B,...,Z") try: # Python 2 str1 = str.upper(raw_input(" number in base "+str(A)+" = ")) except: # Python 3 str1 = str.upper(input(" number in base "+str(A)+" = ")) str2 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" print("") # convert str1 to number number = 0 for i in range(len(str1)): value = str2.index(str1[i]) if value >= A: print("input error") quit() number += A**(len(str1)-i-1) * value print("in base 10: "+str(number)) # convert number to string in final base x = 0 # x+1 will be the length of str3 while number/B**x>=B: x+=1 str3 = '' for i in range(x,-1,-1): # x, x-1, ..., 1, 0 temp = int(number/B**i) number = number - temp * B**i str3 += str2[temp] print("in base "+str(B)+": "+str3) print("")