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 实现归并排序算法
Jun 05 Python
Python中列表和元组的使用方法和区别详解
Dec 30 Python
基于pandas将类别属性转化为数值属性的方法
Jul 25 Python
pygame游戏之旅 添加键盘按键的方法
Nov 20 Python
python画图系列之个性化显示x轴区段文字的实例
Dec 13 Python
Django框架设置cookies与获取cookies操作详解
May 27 Python
python 使用turtule绘制递归图形(螺旋、二叉树、谢尔宾斯基三角形)
May 30 Python
Django的性能优化实现解析
Jul 30 Python
对django2.0 关联表的必填on_delete参数的含义解析
Aug 09 Python
详解python uiautomator2 watcher的使用方法
Sep 09 Python
python 协程 gevent原理与用法分析
Nov 22 Python
Python实现爬取并分析电商评论
Jun 19 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
Discuz Uchome ajaxpost小技巧
2011/01/04 PHP
PHP调用Webservice实例代码
2011/07/29 PHP
Yii框架中 find findAll 查找出制定的字段的方法对比
2014/09/10 PHP
PHP中array_slice函数用法实例详解
2014/11/25 PHP
Laravel框架基于ajax和layer.js实现无刷新删除功能示例
2019/01/17 PHP
TP5框架使用QueryList采集框架爬小说操作示例
2020/03/26 PHP
laravel入门知识点整理
2020/09/15 PHP
Javascript和Ajax中文乱码吐血版解决方案
2009/12/21 Javascript
Javascript数组的排序 sort()方法和reverse()方法
2012/06/04 Javascript
关于js数组去重的问题小结
2014/01/24 Javascript
extjs每个组件要设置唯一的ID否则会出错
2014/06/15 Javascript
jquery delay()介绍及使用指南
2014/09/02 Javascript
JavaScript onkeypress事件入门实例(按下或按住一个键盘按键)
2014/10/17 Javascript
jquery加载图片时以淡入方式显示的方法
2015/01/14 Javascript
浅谈js中变量初始化
2015/02/03 Javascript
jQuery实现仿淘宝带有指示条的图片转动切换效果完整实例
2015/03/04 Javascript
小程序自定义日历效果
2018/12/29 Javascript
vue watch关于对象内的属性监听
2019/04/22 Javascript
JS匿名函数内部this指向问题详析
2019/05/10 Javascript
JavaScript剩余操作符Rest Operator详解
2019/07/20 Javascript
layui实现鼠标移动到单元格上显示数据的方法
2019/09/11 Javascript
jquery实现简单拖拽效果
2020/07/20 jQuery
[59:00]OG vs TNC 2018国际邀请赛小组赛BO2 第一场 8.19
2018/08/21 DOTA
解决谷歌搜索技术文章时打不开网页问题的python脚本
2013/02/10 Python
python利用正则表达式搜索单词示例代码
2017/09/24 Python
Python 读取指定文件夹下的所有图像方法
2018/04/27 Python
python实践项目之监控当前联网状态详情
2019/05/23 Python
Python split() 函数拆分字符串将字符串转化为列的方法
2019/07/16 Python
Python爬虫如何应对Cloudflare邮箱加密
2020/06/24 Python
如何在pycharm中安装第三方包
2020/10/27 Python
python爬虫利器之requests库的用法(超全面的爬取网页案例)
2020/12/17 Python
电脑教师的自我评价
2013/12/18 职场文书
护士辞职信模板
2014/01/20 职场文书
2015年社区工会工作总结
2015/05/26 职场文书
2015年高中语文教学总结
2015/08/18 职场文书
《乌鸦喝水》教学反思
2016/02/19 职场文书