python实现二进制转换其他进制的代码

版权声明:原创 https://blog.csdn.net/qq_41058594/article/details/82666423

1.python二进制转化十进制

s = input() 
d = 0
while s:
    d = d*2 + (ord(s[0]) -ord('0'))
    s = s[1:]
print("转换成八进制数是:{:}".format(d))

2.python二进制转化八进制

s = input() 
d = 0
while s:
    d = d*2 + (ord(s[0]) -ord('0'))
    s = s[1:]
print("转换成八进制数是:{:o}".format(d))

3.python二进制转化十六进制

s = input() 
d = 0
while s:
    d = d*2 + (ord(s[0]) -ord('0'))
    s = s[1:]
print("转换成八进制数是:{:x}".format(d))

其他的进制转化按照以上模板改一下其中的:

print("转换成八进制数是:{:¥}".format(d))

¥处即可!!

猜你喜欢

转载自blog.csdn.net/qq_41058594/article/details/82666423
今日推荐