Python内置函数bin() oct()等实现进制转换


Posted in Python onDecember 30, 2012

使用Python内置函数:bin()、oct()、int()、hex()可实现进制转换。
先看Python官方文档中对这几个内置函数的描述:
bin(x)
Convert an integer number to a binary string. The result is a valid Python expression. If x is not a Python int object, it has to define an __index__() method that returns an integer.
oct(x)
Convert an integer number to an octal string. The result is a valid Python expression. If x is not a Python int object, it has to define an __index__() method that returns an integer.
int([number | string[, base]])
Convert a number or string to an integer. If no arguments are given, return 0. If a number is given, return number.__int__(). Conversion of floating point numbers to integers truncates towards zero. A string must be a base-radix integer literal optionally preceded by ‘+' or ‘-‘ (with no space in between) and optionally surrounded by whitespace. A base-n literal consists of the digits 0 to n-1, with ‘a' to ‘z' (or ‘A' to ‘Z') having values 10 to 35. The default base is 10. The allowed values are 0 and 2-36. Base-2, -8, and -16 literals can be optionally prefixed with 0b/0B, 0o/0O, or 0x/0X, as with integer literals in code. Base 0 means to interpret exactly as a code literal, so that the actual base is 2, 8, 10, or 16, and so that int('010', 0) is not legal, while int('010') is, as well as int('010', 8).
hex(x)
Convert an integer number to a hexadecimal string. The result is a valid Python expression. If x is not a Python int object, it has to define an __index__() method that returns an integer.

2进制 8进制 10进制 16进制
2进制 - bin(int(x, 8)) bin(int(x, 10)) bin(int(x, 16))
8进制 oct(int(x, 2)) - oct(int(x, 10)) oct(int(x, 16))
10进制 int(x, 2) int(x, 8) - int(x, 16)
16进制 hex(int(x, 2)) hex(int(x, 8)) hex(int(x, 10)) -

bin()、oct()、hex()的返回值均为字符串,且分别带有0b、0o、0x前缀。
Python 相关文章推荐
Python调用命令行进度条的方法
May 05 Python
Djang中静态文件配置方法
Jul 30 Python
Python引用模块和查找模块路径
Mar 17 Python
python遍历 truple list dictionary的几种方法总结
Sep 11 Python
python hbase读取数据发送kafka的方法
Dec 27 Python
python随机在一张图像上截取任意大小图片的方法
Jan 24 Python
Python中使用遍历在列表中添加字典遇到的坑
Feb 27 Python
python elasticsearch从创建索引到写入数据的全过程
Aug 04 Python
python批量处理文件或文件夹
Jul 28 Python
python 协程 gevent原理与用法分析
Nov 22 Python
Python Django view 两种return的实现方式
Mar 16 Python
jupyter notebook 多环境conda kernel配置方式
Apr 10 Python
python的id()函数解密过程
Dec 25 #Python
python cookielib 登录人人网的实现代码
Dec 19 #Python
python 多线程应用介绍
Dec 19 #Python
Python多线程学习资料
Dec 19 #Python
python搭建简易服务器分析与实现
Dec 15 #Python
Python笔记(叁)继续学习
Oct 24 #Python
python笔记(2)
Oct 24 #Python
You might like
php中数组首字符过滤功能代码
2012/07/31 PHP
win7+apache+php+mysql环境配置操作详解
2013/06/10 PHP
PHP+redis实现的限制抢购防止商品超发功能详解
2019/09/19 PHP
Easy.Ajax 部分源代码 支持文件上传功能, 兼容所有主流浏览器
2011/02/24 Javascript
获取服务器传来的数据 用JS去空格的正则表达式
2012/03/26 Javascript
原生js实现改变随意改变div属性style的名称和值的结果
2013/09/26 Javascript
JS实现多物体缓冲运动实例代码
2013/11/29 Javascript
jQuery中clone()方法用法实例
2015/01/16 Javascript
属于你的jQuery提示框(Tip)插件
2016/01/20 Javascript
TypeOf这些知识点你了解吗
2016/02/21 Javascript
轻松掌握jQuery中wrap()与unwrap()函数的用法
2016/05/24 Javascript
JavaScript面试题大全(推荐)
2016/09/22 Javascript
微信小程序 数据封装,参数传值等经验分享
2017/01/09 Javascript
Javascript封装id、class与元素选择器方法示例
2017/03/13 Javascript
js+canvas实现动态吃豆人效果
2017/03/22 Javascript
Three.js中网格对象MESH的属性与方法详解
2017/09/27 Javascript
微信小程序实现跟随菜单效果和循环嵌套加载数据
2017/11/21 Javascript
vue自定义指令directive实例详解
2018/01/17 Javascript
vue计算属性时v-for处理数组时遇到的一个bug问题
2018/01/21 Javascript
JS实现求字符串中出现最多次数的字符和次数示例
2019/07/05 Javascript
koa2 从入门到精通(小结)
2019/07/23 Javascript
vux-scroller实现移动端上拉加载功能过程解析
2019/10/08 Javascript
[50:58]2018DOTA2亚洲邀请赛 4.1 小组赛 B组 Mineski vs EG
2018/04/03 DOTA
[01:24:16]2018DOTA2亚洲邀请赛 4.6 全明星赛
2018/04/10 DOTA
python 字符串split的用法分享
2013/03/23 Python
Python选择排序、冒泡排序、合并排序代码实例
2015/04/10 Python
python实现连接mongodb的方法
2015/05/08 Python
Python查询IP地址归属完整代码
2017/06/21 Python
python中可以声明变量类型吗
2020/06/18 Python
keras使用Sequence类调用大规模数据集进行训练的实现
2020/06/22 Python
python两个list[]相加的实现方法
2020/09/23 Python
详解CSS3中border-image的使用
2015/07/18 HTML / CSS
卡塔尔航空官方网站:Qatar Airways
2017/02/08 全球购物
shell程序中如何注释
2012/02/17 面试题
python - asyncio异步编程
2021/04/06 Python
SQLServer2008提示评估期已过解决方案
2021/04/12 SQL Server