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 相关文章推荐
pyqt4教程之实现windows窗口小示例分享
Mar 07 Python
构建Python包的五个简单准则简介
Jun 15 Python
python3.5实现socket通讯示例(TCP)
Feb 07 Python
Python打包方法Pyinstaller的使用
Oct 09 Python
python添加菜单图文讲解
Jun 04 Python
如何利用python给图片添加半透明水印
Sep 06 Python
python multiprocessing多进程变量共享与加锁的实现
Oct 02 Python
Django连接数据库并实现读写分离过程解析
Nov 13 Python
python实现的多任务版udp聊天器功能案例
Nov 13 Python
python加密解密库cryptography使用openSSL生成的密匙加密解密
Feb 11 Python
通过代码实例了解Python异常本质
Sep 16 Python
利用Python实现字幕挂载(把字幕文件与视频合并)思路详解
Oct 21 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函数
2008/10/03 PHP
php字符编码转换之gb2312转为utf8
2013/10/28 PHP
PHP连接MSSQL方法汇总
2016/02/05 PHP
php获取远程图片并下载保存到本地的方法分析
2016/10/08 PHP
PHP实现的Redis多库选择功能单例类
2017/07/27 PHP
php常用字符串长度函数strlen()与mb_strlen()用法实例分析
2019/06/25 PHP
Jquery Ajax学习实例7 Ajax所有过程事件分析示例
2010/03/23 Javascript
JavaScript面向对象之Prototypes和继承
2012/07/12 Javascript
onkeyup,onkeydown和onkeypress的区别介绍
2013/10/21 Javascript
js菜单点击显示或隐藏效果的简单实例
2014/01/13 Javascript
jquery跨域请求示例分享(jquery发送ajax请求)
2014/03/25 Javascript
jQuery Ajax 实例代码 ($.ajax、$.post、$.get)
2016/04/29 Javascript
jquery实现简单Tab切换菜单效果
2020/07/17 Javascript
JavaScript实现瀑布流以及加载效果
2017/02/11 Javascript
Vue+axios 实现http拦截及路由拦截实例
2017/04/25 Javascript
全新打包工具parcel零配置vue开发脚手架
2018/01/11 Javascript
JS代码实现电脑配置检测功能
2018/03/21 Javascript
JS常见DOM节点操作示例【创建 ,插入,删除,复制,查找】
2018/05/14 Javascript
详解Python中映射类型的内建函数和工厂函数
2015/08/19 Python
Python读取和处理文件后缀为.sqlite的数据文件(实例讲解)
2017/06/27 Python
python实现简易内存监控
2018/06/21 Python
Windows下python3.7安装教程
2018/07/31 Python
python找出完数的方法
2018/11/12 Python
python实现简单的文字识别
2018/11/27 Python
opencv-python 提取sift特征并匹配的实例
2019/12/09 Python
解决keras backend 越跑越慢问题
2020/06/18 Python
python 读txt文件,按‘,’分割每行数据操作
2020/07/05 Python
解决Windows下python和pip命令无法使用的问题
2020/08/31 Python
英国马莎百货官网:Marks & Spencer
2016/07/29 全球购物
玖熙女鞋美国官网:Nine West
2016/10/06 全球购物
科技活动周标语
2014/10/08 职场文书
团委工作总结2015
2015/04/02 职场文书
2016元旦文艺汇演主持词(开场白+结束语)
2015/12/03 职场文书
2016民族团结先进个人事迹材料
2016/02/26 职场文书
详解如何在Canvas中添加事件的方法
2021/04/17 Javascript
Java实战之课程信息管理系统的实现
2022/04/01 Java/Android