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之不要红头文件(1)
Sep 28 Python
python根据路径导入模块的方法
Sep 30 Python
python执行外部程序的常用方法小结
Mar 21 Python
从Python的源码来解析Python下的freeblock
May 11 Python
Django框架下在视图中使用模版的方法
Jul 16 Python
python itchat实现微信好友头像拼接图的示例代码
Aug 14 Python
python如何实现反向迭代
Mar 20 Python
Python实现检测文件MD5值的方法示例
Apr 11 Python
python使用Qt界面以及逻辑实现方法
Jul 10 Python
python写程序统计词频的方法
Jul 29 Python
selenium中get_cookies()和add_cookie()的用法详解
Jan 06 Python
python入门:argparse浅析 nargs='+'作用
Jul 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操作sqlserver关于时间日期读取的小小见解
2009/11/29 PHP
PHP array_push 数组函数
2009/12/26 PHP
php中CI操作多个数据库的代码
2012/07/05 PHP
PHP正确解析UTF-8字符串技巧应用
2012/11/07 PHP
PHP字符串比较函数strcmp()和strcasecmp()使用总结
2014/11/19 PHP
javascript新手语法小结
2008/06/15 Javascript
javascript cookie操作类的实现代码小结附使用方法
2010/06/02 Javascript
jqTransform form表单美化插件使用方法
2012/07/05 Javascript
浅谈Node.js中的定时器
2015/06/18 Javascript
基于jQuery实现简单的折叠菜单效果
2015/11/23 Javascript
jQuery使用each方法与for语句遍历数组示例
2016/06/16 Javascript
用js读写cookie的简单方法(推荐)
2016/08/08 Javascript
JavaScript拖动层Div代码
2017/03/01 Javascript
jQuery鼠标移动图片上实现放大效果
2017/06/25 jQuery
基于nodejs实现微信支付功能
2017/12/20 NodeJs
vue+express 构建后台管理系统的示例代码
2018/07/19 Javascript
vue3.0 项目搭建和使用流程
2021/03/04 Vue.js
[02:00]最后,我终于出了辉耀
2018/03/27 DOTA
python字符串的方法与操作大全
2018/01/30 Python
wx.CheckBox创建复选框控件并响应鼠标点击事件
2018/04/25 Python
Python实现正弦信号的时域波形和频谱图示例【基于matplotlib】
2018/05/04 Python
python 3.3 下载固定链接文件并保存的方法
2018/12/18 Python
python引用(import)某个模块提示没找到对应模块的解决方法
2019/01/19 Python
python之当你发现QTimer不能用时的解决方法
2019/06/21 Python
pygame实现打字游戏
2021/02/19 Python
wxpython自定义下拉列表框过程图解
2020/02/14 Python
利用python批量爬取百度任意类别的图片的实现方法
2020/10/07 Python
HTML5之web workers_动力节点Java学院整理
2017/07/17 HTML / CSS
加拿大时尚少女服装品牌:Garage
2016/10/10 全球购物
香港草莓网:Strawberrynet香港
2019/05/10 全球购物
高中生毕业自我鉴定
2013/10/10 职场文书
财务主管的岗位职责
2013/12/30 职场文书
计生办班子群众路线教育实践活动个人对照检查材料思想汇报
2014/10/04 职场文书
交通事故委托书范本精选
2014/10/04 职场文书
道路交通事故赔偿协议书
2014/10/24 职场文书
基层医务人员三严三实心得体会
2016/01/05 职场文书