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实现学校管理系统
Jan 11 Python
详解python多线程、锁、event事件机制的简单使用
Apr 27 Python
python面向对象多线程爬虫爬取搜狐页面的实例代码
May 31 Python
Python Tkinter模块实现时钟功能应用示例
Jul 23 Python
基于Python的ModbusTCP客户端实现详解
Jul 13 Python
在pycharm下设置自己的个性模版方法
Jul 15 Python
使用Filter过滤python中的日志输出的实现方法
Jul 17 Python
python使用HTMLTestRunner导出饼图分析报告的方法
Dec 30 Python
python开发前景如何
Jun 11 Python
Python requests及aiohttp速度对比代码实例
Jul 16 Python
python opencv实现简易画图板
Aug 27 Python
基于注解实现 SpringBoot 接口防刷的方法
Mar 02 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
smarty section简介与用法分析
2008/10/03 PHP
使用淘宝IP库获取用户ip地理位置
2013/10/27 PHP
Yii2汉字转拼音类的实例代码
2017/04/18 PHP
IE与firefox下Dhtml的一些区别小结
2009/12/02 Javascript
CSS+Jquery实现页面圆角框方法大全
2009/12/24 Javascript
JSON 学习之JSON in JavaScript详细使用说明
2010/02/23 Javascript
基于jquery的无刷新分页技术
2011/06/11 Javascript
jQuery中delegate与on的用法与区别示例介绍
2013/12/20 Javascript
js返回上一页并刷新的多种实现方法
2014/02/26 Javascript
基于Jquery+Ajax+Json实现分页显示附效果图
2014/07/30 Javascript
BOOTSTRAP时间控件显示在模态框下面的bug修复
2015/02/05 Javascript
JavaScript里四舍五入函数round用法实例
2015/04/06 Javascript
快速获取/设置iframe内对象元素的几种js实现方法
2016/05/20 Javascript
JS获取屏幕高度的简单实现代码
2016/05/24 Javascript
jQuery解析XML 详解及方法总结
2016/09/28 Javascript
Bootstrap的aria-label和aria-labelledby属性实例详解
2018/11/02 Javascript
Taro UI框架开发小程序实现左滑喜欢右滑不喜欢效果的示例代码
2020/05/18 Javascript
vue中touch和click共存的解决方式
2020/07/28 Javascript
浅析JavaScript 函数柯里化
2020/09/08 Javascript
JavaScript实现滚动加载更多
2020/12/27 Javascript
Python redis操作实例分析【连接、管道、发布和订阅等】
2019/05/16 Python
使用PyQt的QLabel组件实现选定目标框功能的方法示例
2020/05/19 Python
基于SQLAlchemy实现操作MySQL并执行原生sql语句
2020/06/10 Python
基于python实现坦克大战游戏
2020/10/27 Python
html5中canvas学习笔记1-画板的尺寸与实际显示尺寸
2013/01/06 HTML / CSS
html5使用canvas画三角形
2014/12/15 HTML / CSS
canvas 实现 github404动态效果的示例代码
2017/11/15 HTML / CSS
详解如何解决canvas图片getImageData,toDataURL跨域问题
2018/09/17 HTML / CSS
Nike墨西哥官网:Nike MX
2020/08/30 全球购物
学校运动会开幕演讲稿
2014/01/04 职场文书
母亲七十大寿答谢词
2014/01/18 职场文书
教师群众路线教育实践活动个人对照检查材料
2014/11/04 职场文书
公积金具结保证书
2015/05/11 职场文书
五星红旗迎风飘扬观后感
2015/06/17 职场文书
解决numpy和torch数据类型转化的问题
2021/05/23 Python
Java 定时任务技术趋势简介
2022/05/04 Java/Android