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爬虫_自动获取seebug的poc实例
Aug 05 Python
python根据文章标题内容自动生成摘要的实例
Feb 21 Python
Django项目中添加ldap登陆认证功能的实现
Apr 04 Python
Python socket模块实现的udp通信功能示例
Apr 10 Python
python pytest进阶之xunit fixture详解
Jun 27 Python
PyCharm中代码字体大小调整方法
Jul 29 Python
Pandas时间序列:重采样及频率转换方式
Dec 26 Python
python能做哪方面的工作
Jun 15 Python
python脚本定时发送邮件
Dec 22 Python
详解appium自动化测试工具(monitor、uiautomatorviewer)
Jan 27 Python
Python 制作自动化翻译工具
Apr 25 Python
python实现剪贴板的操作
Jul 01 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
海贼王动画变成“真人”后,凯多神还原,雷利太帅了!
2020/04/09 日漫
php file_exists 检查文件或目录是否存在的函数
2010/05/10 PHP
php 变量未定义等错误的解决方法
2011/01/12 PHP
PHP的拦截器实例分析
2014/11/03 PHP
PHP实现简单搜歌的方法
2015/07/28 PHP
CI框架数据库查询之join用法分析
2016/05/18 PHP
PHP标准类(stdclass)用法示例
2016/09/28 PHP
PHP观察者模式示例【Laravel框架中有用到】
2018/06/15 PHP
php操作redis数据库常见方法实例总结
2020/02/20 PHP
TextArea 控件的最大长度问题(js json)
2009/12/16 Javascript
javascript中的prototype属性使用说明(函数功能扩展)
2010/08/16 Javascript
TinyMCE 新增本地图片上传功能
2010/11/05 Javascript
StringTemplate遇见jQuery冲突的解决方法
2011/09/22 Javascript
基于JQuery的模拟苹果桌面Dock效果(稳定版)
2012/10/15 Javascript
jquery select动态加载选择(兼容各种浏览器)
2013/02/01 Javascript
适用于手机端的jQuery图片滑块动画
2016/12/09 Javascript
JS ES6中setTimeout函数的执行上下文示例
2017/04/27 Javascript
js实现canvas保存图片为png格式并下载到本地的方法
2017/08/31 Javascript
教你如何用node连接redis的示例代码
2018/07/12 Javascript
详解如何在vscode里面调试js和node.js的方法步骤
2018/12/24 Javascript
微信小程序云开发如何实现数据库自动备份实现
2019/08/16 Javascript
vue+webpack dev本地调试全局样式引用失效的解决方案
2019/11/12 Javascript
js实现菜单跳转效果
2020/12/11 Javascript
Python实现的归并排序算法示例
2017/11/21 Python
使用Python+Splinter自动刷新抢12306火车票
2018/01/03 Python
python读取TXT每行,并存到LIST中的方法
2018/10/26 Python
关于python中导入文件到list的问题
2020/10/31 Python
使用HTML5 Canvas API中的clip()方法裁剪区域图像
2016/03/25 HTML / CSS
39美元购买一副眼镜或太阳镜:39DollarGlasses.com
2018/06/17 全球购物
英国家用电器折扣网站:Electrical Discount UK
2018/09/17 全球购物
卫校中专生个人自我评价
2013/09/19 职场文书
简历中个人自我评价范文
2013/12/26 职场文书
学校党的群众路线教育实践活动制度建设计划
2014/11/03 职场文书
2016年优秀共青团员事迹材料
2016/02/25 职场文书
设置IIS Express并发数
2022/07/07 Servers
Java获取字符串编码格式实现思路
2022/09/23 Java/Android