Da base 2…9 a base 10
def c_b_10(x,b):
y=0
for c in x:
p=int(c)
y=y*b+p
return y
numero = input("Numero = ")
base1 = int(input("Da base (2..9) = "))
risp = c_b_10(numero,base1)
print(risp)
Da base 2…36 a base 10
Ogni cifra (‘0′,…,’9′,’A’,…,’Z’) deve essere convertita nel suo peso in base 10 (0,…,35)
def peso(car):
if(car.isdigit()):
return int(car)
else:
return 10+ord(car)-ord("A")
def c_b_10(x,b):
x=x.upper()
y=0
for c in x:
p=peso(c)
y=y*b+p
return y
numero = input("Numero = ")
base1 = int(input("Da base (2..36) = "))
risp = c_b_10(numero,base1)
print(risp)
Da base 10 a base 2…9
def c_10_b(x,b):
if(x == 0):
return "0"
else:
y=""
while(x > 0):
q=x//b
r=x%b
y=str(r)+y
x=q
return y
numero = int(input("Numero = "))
base2 = int(input("A base (2..9) = "))
risp = c_10_b(numero,base2)
print(risp)
Da base 10 a base 2…36
Il peso della cifra (0…35) diventa un carattere (‘0’…’9′,’A’…’Z’)
def cifra(c):
if(c < 10):
return str(c)
else:
return chr(ord("A")+c-10)
def c_10_b(x,b):
if(x == 0):
return "0"
else:
y=""
while(x > 0):
q=x//b
r=x%b
y=cifra(r)+y
x=q
return y
numero = int(input("Numero = "))
base2 = int(input("A base (2..36) = "))
risp = c_10_b(numero,base2)
print(risp)
Da base 2…36 a base 2…36
Si passa per la base 10…
def peso(car):
if(car.isdigit()):
return int(car)
else:
return 10+ord(car)-ord("A")
def cifra(c):
if(c < 10):
return str(c)
else:
return chr(ord("A")+c-10)
def c_10_b(x,b):
if(x == 0):
return "0"
else:
y=""
while(x > 0):
q=x//b
r=x%b
y=cifra(r)+y
x=q
return y
def c_b_10(x,b):
x=x.upper()
y=0
for c in x:
p=peso(c)
y=y*b+p
return y
numero = input("Numero = ")
base1 = int(input("Da base = "))
base2 = int(input("A base = "))
risp = c_b_10(numero,base1);
print(risp)
risp = c_10_b(risp ,base2);
print(risp)