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进阶教程之循环相关函数range、enumerate、zip
Aug 30 Python
pymongo实现控制mongodb中数字字段做加法的方法
Mar 26 Python
你应该知道的python列表去重方法
Jan 17 Python
用python制作游戏外挂
Jan 04 Python
python绘制多个曲线的折线图
Mar 23 Python
对pytorch网络层结构的数组化详解
Dec 08 Python
Python饼状图的绘制实例
Jan 15 Python
Python两个字典键同值相加的几种方法
Mar 05 Python
对python3.4 字符串转16进制的实例详解
Jun 12 Python
python 实现绘制整齐的表格
Nov 18 Python
Tensorflow Summary用法学习笔记
Jan 10 Python
java关于string最常出现的面试题整理
Jan 18 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
PHP5 字符串处理函数大全
2010/03/23 PHP
深入php define()函数以及defined()函数的用法详解
2013/06/05 PHP
解析php中var_dump,var_export,print_r三个函数的区别
2013/06/21 PHP
使用 laravel sms 构建短信验证码发送校验功能
2017/11/06 PHP
php打开本地exe程序,js打开本地exe应用程序,并传递相关参数方法
2018/02/06 PHP
js获取当前select 元素值的代码
2010/04/19 Javascript
判断客户端浏览器是否安装了Flash插件的多种方法
2010/08/11 Javascript
Jquery公告滚动+AJAX后台得到数据
2011/04/14 Javascript
键盘上一张下一张兼容IE/google/firefox等浏览器
2014/01/28 Javascript
button没写type=button会导致点击时提交
2014/03/06 Javascript
jQuery遍历对象、数组、集合实例
2014/11/08 Javascript
详解AngularJS通过ocLazyLoad实现动态(懒)加载模块和依赖
2017/03/01 Javascript
浅析webpack 如何优雅的使用tree-shaking(摇树优化)
2017/08/16 Javascript
jQuery实现手机号正则验证输入及自动填充空格功能
2018/01/02 jQuery
JS处理一些简单计算题
2018/02/24 Javascript
对layui中的onevent 和event的使用详解
2019/09/06 Javascript
JS插入排序简单理解与实现方法分析
2019/11/25 Javascript
解决VUE项目使用Element-ui 下拉组件的验证失效问题
2020/11/07 Javascript
Python去除列表中重复元素的方法
2015/03/20 Python
详解Python操作RabbitMQ服务器消息队列的远程结果返回
2016/06/30 Python
Win10下python 2.7.13 安装配置方法图文教程
2018/09/18 Python
10 分钟快速入门 Python3的教程
2019/01/29 Python
python使用threading.Condition交替打印两个字符
2019/05/07 Python
flask框架json数据的拿取和返回操作示例
2019/11/28 Python
超级实用的8个Python列表技巧
2020/08/24 Python
Python高阶函数与装饰器函数的深入讲解
2020/11/10 Python
四年大学生活的个人自我评价
2013/12/11 职场文书
读书心得体会
2013/12/28 职场文书
数学系毕业生的自我评价
2014/01/10 职场文书
幼儿园开学家长寄语
2014/01/19 职场文书
区优秀教师事迹材料
2014/02/10 职场文书
新闻编辑专业自荐信
2014/07/02 职场文书
医生学习党的群众路线教育实践活动心得体会
2014/11/03 职场文书
2015年社区纪检工作总结
2015/04/21 职场文书
开除员工通知
2015/04/22 职场文书
试用期转正工作总结2015
2015/05/28 职场文书