#!/usr/bin/python
# CODIGO CON DOS FUNCIONES. CONVIERTE NUMEROS A VARIAS BASES. INTRODUCES UN NUMERO Y LA BASE EN LA QUE LO QUIERES.
def digit_to_char(digit):
if digit < 10:
return chr(ord('0') + digit)
else:
return chr(ord('a') + digit - 10)
def str_base(number,base):
if number < 0:
return '-' + str_base(-number,base)
else:
(d,m) = divmod(number,base)
if d:
return str_base(d,base) + digit_to_char(m)
else:
return digit_to_char(m)
a = int(input("introduzca numero a convertir a cualquier base: "))
b = int(input("introduzca base: "))
print str_base(a,b)
Veamos un ejemplo sencillo.
Vamos a convertir el número 145 a base 3 y después a base 2.
Introduzca numero a convertir a cualquier base: 145 Introduzca base: 3
12101
Introduzca numero a convertir a cualquier base: 145 Introduzca base: 2