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中的进程分支fork和exec详解
Apr 11 Python
python求解水仙花数的方法
May 11 Python
Python生成不重复随机值的方法
May 11 Python
TensorFlow如何实现反向传播
Feb 06 Python
Python使用jsonpath-rw模块处理Json对象操作示例
Jul 31 Python
Django框架封装外部函数示例
May 28 Python
python3 mmh3安装及使用方法
Oct 09 Python
tensorflow实现测试时读取任意指定的check point的网络参数
Jan 21 Python
Python API len函数操作过程解析
Mar 05 Python
浅谈Python线程的同步互斥与死锁
Mar 22 Python
python属于解释语言吗
Jun 11 Python
解决Tensorflow2.0 tf.keras.Model.load_weights() 报错处理问题
Jun 12 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维护文件系统
2006/10/09 PHP
PHP实现对png图像进行缩放的方法(支持透明背景)
2015/07/15 PHP
Google Map V3 绑定气泡窗口(infowindow)Dom事件实现代码
2013/04/26 Javascript
jquery自定义类似$.ajax()的方法实现代码
2013/08/13 Javascript
js 实现菜单左右滚动显示示例介绍
2013/11/21 Javascript
JavaScript对象反射用法实例
2015/04/17 Javascript
jquery实现的动态回到顶部特效代码
2015/10/28 Javascript
原生JS实现旋转木马式图片轮播插件
2016/04/25 Javascript
js 判断一组日期是否是连续的简单实例
2016/07/11 Javascript
全面介绍javascript实用技巧及单竖杠
2016/07/18 Javascript
JavaScript制作简单分页插件
2016/09/11 Javascript
bootstrap中使用google prettify让代码高亮的方法
2016/10/21 Javascript
JS重载实现方法分析
2016/12/16 Javascript
JQuery实现文字无缝滚动效果示例代码(Marquee插件)
2017/03/07 Javascript
JS组件系列之JS组件封装过程详解
2017/04/28 Javascript
Easyui使用Dialog行内按钮布局的实例
2017/07/27 Javascript
10个经典的网页鼠标特效代码
2018/01/09 Javascript
10 种最常见的 Javascript 错误(频率最高)
2018/02/08 Javascript
[01:14:55]EG vs Spirit Supermajor 败者组 BO3 第三场 6.4
2018/06/05 DOTA
将图片文件嵌入到wxpython代码中的实现方法
2014/08/11 Python
python检查指定文件是否存在的方法
2015/07/06 Python
Python字符串切片操作知识详解
2016/03/28 Python
Python轻量级ORM框架Peewee访问sqlite数据库的方法详解
2017/07/20 Python
Python如何抓取天猫商品详细信息及交易记录
2018/02/23 Python
在Python中构建增广矩阵的实现方法
2019/07/01 Python
Django-Model数据库操作(增删改查、连表结构)详解
2019/07/17 Python
python虚拟环境完美部署教程
2019/08/06 Python
Python字典常见操作实例小结【定义、添加、删除、遍历】
2019/10/25 Python
pygame实现贪吃蛇游戏(上)
2019/10/29 Python
Python 剪绳子的多种思路实现(动态规划和贪心)
2020/02/24 Python
pandas中的ExcelWriter和ExcelFile的实现方法
2020/04/24 Python
使paramiko库执行命令时在给定的时间强制退出功能的实现
2021/03/03 Python
法律专业个人实习自我鉴定
2013/09/23 职场文书
成人高等教育毕业生自我鉴定
2013/10/22 职场文书
鸟的天堂导游词
2015/01/31 职场文书
Redis 的查询很快的原因解析及Redis 如何保证查询的高效
2022/03/16 Redis