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文件和目录操作详解
Feb 08 Python
python实现的守护进程(Daemon)用法实例
Jun 02 Python
使用Kivy将python程序打包为apk文件
Jul 29 Python
用Python进行简单图像识别(验证码)
Jan 19 Python
windows系统中Python多版本与jupyter notebook使用虚拟环境的过程
May 15 Python
python 列表输出重复值以及对应的角标方法
Jun 11 Python
PYQT5实现控制台显示功能的方法
Jun 25 Python
Python中一个for循环循环多个变量的示例
Jul 16 Python
python实现通过flask和前端进行数据收发
Aug 22 Python
Python安装并操作redis实现流程详解
Oct 13 Python
python 模拟登录B站的示例代码
Dec 15 Python
golang特有程序结构入门教程
Jun 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
Codeigniter操作数据库表的优化写法总结
2014/06/12 PHP
PHP反向代理类代码
2014/08/15 PHP
PHP中Header使用的HTTP协议及常用方法小结
2014/11/04 PHP
php使用curl获取https请求的方法
2015/02/11 PHP
RR vs IO BO3 第二场2.13
2021/03/10 DOTA
JS支持带x身份证号码验证函数
2008/08/10 Javascript
JS 时间显示效果代码
2009/08/23 Javascript
javascript 面向对象继承
2009/11/26 Javascript
JS操作CSS随机改变网页背景实现思路
2014/03/10 Javascript
NodeJS Web应用监听sock文件实例
2015/02/18 NodeJs
json的结构与遍历方法实例分析
2017/04/25 Javascript
JavaScript引用类型之基本包装类型实例分析【Boolean、Number和String】
2018/08/09 Javascript
在Create React App中使用CSS Modules的方法示例
2019/01/15 Javascript
微信小程序:数据存储、传值、取值详解
2019/05/07 Javascript
[06:11]2014DOTA2国际邀请赛 专访团结一心的VG战队
2014/07/21 DOTA
[01:05:30]VP vs TNC 2018国际邀请赛小组赛BO2 第一场 8.17
2018/08/20 DOTA
django进阶之cookie和session的使用示例
2018/08/17 Python
Python 面向对象静态方法、类方法、属性方法知识点小结
2020/03/09 Python
python datetime处理时间小结
2020/04/16 Python
使用python把xmind转换成excel测试用例的实现代码
2020/10/12 Python
Pandas DataFrame求差集的示例代码
2020/12/13 Python
css3动画鼠标放上图片逐渐变大鼠标离开图片逐渐缩小效果
2021/01/27 HTML / CSS
欧洲最大的化妆品连锁公司:Douglas道格拉斯
2017/05/06 全球购物
瑞典手机壳品牌:Richmond & Finch
2018/04/28 全球购物
微软马来西亚官方网站:Microsoft马来西亚
2019/11/22 全球购物
体育专业个人的求职信范文
2013/09/21 职场文书
中专生求职自荐信范文
2013/12/22 职场文书
《月光启蒙》教学反思
2014/03/01 职场文书
公司节能减排倡议书
2014/05/14 职场文书
公司试用期员工自我评价
2014/09/17 职场文书
反邪教警示教育活动总结
2015/05/09 职场文书
2015年信息技术教研组工作总结
2015/07/22 职场文书
中小学教师继续教育心得体会
2016/01/19 职场文书
煤矿安全生产管理协议书
2016/03/22 职场文书
python unittest单元测试的步骤分析
2021/08/02 Python
Centos系统通过Docker安装并搭建MongoDB数据库
2022/04/12 MongoDB