码迷,mamicode.com
首页 > 编程语言 > 详细

各种进制转换详解-python

时间:2019-01-10 21:49:33      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:进制转换   pop   局限   十六进制   tis   data   key   word   描述   


(1)各个进制的符号:b:二进制;o:八进制;d:十进制;x:十六进制
在python中,bin(),oct(),hex()返回值均为字符串而且会带有0b,0o,0o前缀
技术分享图片
(2)各个进制相互转换
a)十进制转换二进制:
技术分享图片
十进制转换二进制:

#coding=utf-8
s = 10
list_one = []
if s >= 0 and s <= 1:
    print "二进制:%d"%(s)
else:
    while s >= 1:
        list_one.append(str(s % 2))
        s = s / 2
    #list_one.append(str(s))
    list_one.reverse()
print ‘‘.join(list_one)

b)十进制转换八进制:
技术分享图片
十进制转换到八进制

#coding=utf-8
s = 10
list_one = []
if s >= 0 and s <= 1:
    print "二进制:%d"%(s)
else:
    while s >= 1:
        list_one.append(str(s % 8))
        s = s / 8
    #list_one.append(str(s))
    list_one.reverse()
print ‘‘.join(list_one)

c)十进制到十六进制:
技术分享图片
(3)从二,八,十六进制到转换到十进制
a)二进制到十进制:
技术分享图片
b)八进制到十进制
技术分享图片
c)十六进制到十进制
技术分享图片
总结:这里用到格式化字符串函数format;还有eval函数。各个进制之间转换比较灵活不要只局限上面的方法哦;还可以利用我们的公式来转换,或者借用十进制作为中介。

各种进制转换详解-python

标签:进制转换   pop   局限   十六进制   tis   data   key   word   描述   

原文地址:https://www.cnblogs.com/laogao123/p/10252294.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!