Python实现2~32进制转换
2-10进制转换
a, b, c, txt = int(input("输入进制:")), input("转换的数字:"), int(input("输出进制:")), ""
result = sum(a ** i * int(b[::-1][i]) for i in range(len(b))) # 2-10任意进制 转 10进制
while bool(result): # 10进制 转 2-10任意进制
txt += str(result % c)
result //= c
print(txt[::-1])
2-32进制转换
a, b, c, txt = int(input("输入进制:")), input("转换的数字:"), int(input("输出进制:")), ""
lis = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U']
result = sum(a ** i * lis.index(b.upper()[::-1][i]) for i in range(len(b))) # 2-32任意进制 转 10进制
while bool(result): # 10进制 转 2-32任意进制
txt += lis[result % c]
result //= c
print(txt[::-1])