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中针对函数处理的特殊方法
Mar 06 Python
记录Django开发心得
Jul 16 Python
python求列表交集的方法汇总
Nov 10 Python
介绍Python中的__future__模块
Apr 27 Python
Python的Django框架中的URL配置与松耦合
Jul 15 Python
Python两个内置函数 locals 和globals(学习笔记)
Aug 28 Python
python+pyqt5实现24点小游戏
Jan 24 Python
pytorch之ImageFolder使用详解
Jan 06 Python
安装多个版本的TensorFlow的方法步骤
Apr 21 Python
Python网页解析器使用实例详解
May 30 Python
使用keras实现孪生网络中的权值共享教程
Jun 11 Python
pytorch SENet实现案例
Jun 24 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 Session 变量的使用方法详解与实例代码
2013/09/11 PHP
php 判断是否是中文/英文/数字示例代码
2013/09/30 PHP
PHP数据库连接mysql与mysqli对比分析
2016/01/04 PHP
php脚本运行时的超时机制详解
2016/02/17 PHP
Gambit vs CL BO3 第二场 2.13
2021/03/10 DOTA
通用于ie和firefox的函数 GetCurrentStyle (obj, prop)
2006/12/27 Javascript
JS 获取span标签中的值的代码 支持ie与firefox
2009/08/24 Javascript
使用jQuery快速解决input中placeholder值在ie中无法支持的问题
2014/01/02 Javascript
JavaScript避免代码的重复执行经验技巧分享
2014/04/17 Javascript
jQuery文件上传插件Uploadify使用指南
2014/06/05 Javascript
javascript实现框架高度随内容改变的方法
2015/07/23 Javascript
javascript中的正则表达式使用详解
2015/08/30 Javascript
学习javascript面向对象 javascript实现继承的方式
2016/01/04 Javascript
Javascript缓存API
2016/06/14 Javascript
JavaScript提高加载和执行效率的方法
2017/02/03 Javascript
详解使用nvm安装node.js
2017/07/18 Javascript
JavaScript 中定义函数用 var foo = function () {} 和 function foo()区别介绍
2018/03/01 Javascript
vue自定v-model实现表单数据双向绑定问题
2018/09/03 Javascript
jQuery实现全选、反选和不选功能的方法详解
2019/12/04 jQuery
Angular进行简单单元测试的实现方法实例
2020/08/16 Javascript
原生js实现自定义滚动条
2021/01/20 Javascript
[02:05]DOTA2完美大师赛趣味视频之看我表演
2017/11/18 DOTA
Python中的Descriptor描述符学习教程
2016/06/02 Python
python实战串口助手_解决8串口多个发送的问题
2019/06/12 Python
python实现爬取百度图片的方法示例
2019/07/06 Python
在vscode中配置python环境过程解析
2019/09/28 Python
详解Python可视化神器Yellowbrick使用
2019/11/11 Python
ZWILLING双立人法国网上商店:德国刀具锅具厨具品牌
2019/08/28 全球购物
MYSQL基础面试题
2012/05/13 面试题
业务经理的岗位职责
2013/11/16 职场文书
食品采购员岗位职责
2014/04/14 职场文书
《红军不怕远征难》教学反思
2014/04/14 职场文书
考生诚信考试承诺书
2014/05/23 职场文书
2014年教师德育工作总结
2014/11/10 职场文书
2015年保洁工作总结范文
2015/04/28 职场文书
react 路由Link配置详解
2021/11/11 Javascript