教程集 www.jiaochengji.com
教程集 >  Python编程  >  Python入门  >  正文 举例说明Python中常见的数制转换

举例说明Python中常见的数制转换

发布时间:2021-12-29   编辑:jiaochengji.com
教程集为您提供举例说明Python中常见的数制转换等资源,欢迎您收藏本站,我们将为您提供最新的举例说明Python中常见的数制转换资源

数制转换即进制转换,指进制(二、八、十、十六进制)间的相互转换,计算机编程中较为常见。这里列举了python常见数制转换用法。

1.进位制度

Python中二进制是以0b开头的:

例如: 0b11 则表示十进制的3

8进制是以0开头的:

例如: 011则表示十进制的9

16进制是以0x开头的:

例如: 0x11则表示十进制的17

或者写成 \x \b

2.各种函数转换

<pre class="brush:php;toolbar:false">#10进制转为2进制 >>> bin(10) '0b1010'</pre><pre class="brush:php;toolbar:false">#2进制转为10进制 >>> int("1001",2) 9</pre><pre class="brush:php;toolbar:false">#10进制转为16进制 >>> hex(10) '0xa'</pre><pre class="brush:php;toolbar:false">#16进制到10进制 >>> int('ff', 16) 255</pre><pre class="brush:php;toolbar:false">>>> int('0xab', 16) 171</pre><pre class="brush:php;toolbar:false">#十进制转为八进制 >>print("%o" % 10) >>12</pre><pre class="brush:php;toolbar:false">#16进制到2进制 >>> bin(0xa) '0b1010' >>></pre><pre class="brush:php;toolbar:false">#10进制到8进制 >>> oct(8) '010'</pre><pre class="brush:php;toolbar:false">#2进制到16进制 >>> hex(0b1001) '0x9'</pre>

您可能感兴趣的文章:
举例说明Python中常见的数制转换
python百分号什么意思
Python常用模块之hashlib
Int32.Parse()、Convert.toInt32()、Int32.TryParse()
Python的特点(优点和缺点)
十个Python常用的图像处理工具
C语言中变量定义/声明深入分析
Python中格式化的两种方法
javascript中的Number()、parseInt()和parseFloat()数值转换
cython与python的不同有哪些

[关闭]
~ ~