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实现封装得到virustotal扫描结果
Oct 05 Python
python有证书的加密解密实现方法
Nov 19 Python
使用Python的Scrapy框架十分钟爬取美女图
Dec 26 Python
Python简单删除列表中相同元素的方法示例
Jun 12 Python
用 Python 爬了爬自己的微信朋友(实例讲解)
Aug 25 Python
tensorflow实现简单的卷积神经网络
May 24 Python
Python编程学习之如何判断3个数的大小
Aug 07 Python
Python如何调用JS文件中的函数
Aug 16 Python
python如何实现复制目录到指定目录
Feb 13 Python
PyCharm MySQL可视化Database配置过程图解
Jun 09 Python
python保存大型 .mat 数据文件报错超出 IO 限制的操作
May 10 Python
python numpy中multiply与*及matul 的区别说明
May 26 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
《神奇女侠:血脉》神力女超人大战犯罪公司
2020/04/09 欧美动漫
php 字符串中的\n换行符无效、不能换行的解决方法
2014/04/02 PHP
跨浏览器PHP下载文件名中的中文乱码问题解决方法
2015/03/05 PHP
详解php几行代码实现CSV格式文件输出
2017/07/01 PHP
Javascript操纵Cookie实现购物车程序
2007/02/15 Javascript
JavaScript prototype对象的属性说明
2010/03/13 Javascript
Javascript 检测键盘按键信息及键码值对应介绍
2013/01/03 Javascript
js实现广告漂浮效果的小例子
2013/07/02 Javascript
JavaScript中的类数组对象介绍
2014/12/30 Javascript
React Native实现简单的登录功能(推荐)
2016/09/19 Javascript
Vue.js常用指令汇总(v-if、v-for等)
2016/11/03 Javascript
JavaScript运动框架 解决防抖动问题、悬浮对联(二)
2017/05/17 Javascript
vue嵌套路由与404重定向实现方法分析
2018/05/04 Javascript
Vuex的基本概念、项目搭建以及入坑点
2018/11/04 Javascript
JavaScript学习笔记之DOM操作实例分析
2019/01/08 Javascript
ES6的解构赋值实例详解
2019/05/06 Javascript
JavaScript实现单英文金山打字通
2020/07/24 Javascript
Vue实现验证码功能
2019/12/03 Javascript
JS面向对象编程基础篇(一) 对象和构造函数实例详解
2020/03/03 Javascript
基于JavaScript或jQuery实现网站夜间/高亮模式
2020/05/30 jQuery
python通过pil模块将raw图片转换成png图片的方法
2015/03/16 Python
利用Python的装饰器解决Bottle框架中用户验证问题
2015/04/24 Python
Python中defaultdict与lambda表达式用法实例小结
2018/04/09 Python
在Python中append以及extend返回None的例子
2019/07/20 Python
Django-xadmin+rule对象级权限的实现方式
2020/03/30 Python
python实现扫雷游戏的示例
2020/10/20 Python
css3的@media属性实现页面响应式布局示例代码
2014/02/10 HTML / CSS
h5页面背景图很长要有滚动条滑动效果的实现
2021/01/27 HTML / CSS
业务助理岗位职责
2013/11/18 职场文书
战友聚会邀请函
2014/01/18 职场文书
红旗方阵解说词
2014/02/12 职场文书
会计助理岗位职责
2014/02/17 职场文书
工作态度不端正检讨书
2014/10/04 职场文书
先进班集体事迹材料
2014/12/25 职场文书
2015年三八妇女节活动总结
2015/02/06 职场文书
MySql数据库触发器使用教程
2022/06/01 MySQL