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 paramiko实现ssh远程访问的方法
Dec 03 Python
13个最常用的Python深度学习库介绍
Oct 28 Python
Python 操作 ElasticSearch的完整代码
Aug 04 Python
将pytorch转成longtensor的简单方法
Feb 18 Python
解决ROC曲线画出来只有一个点的问题
Feb 28 Python
解决python3插入mysql时内容带有引号的问题
Mar 02 Python
django处理select下拉表单实例(从model到前端到post到form)
Mar 13 Python
Pyinstaller 打包发布经验总结
Jun 02 Python
tensorflow之读取jpg图像长和宽实例
Jun 18 Python
如何通过Python实现RabbitMQ延迟队列
Nov 28 Python
python中sys模块的介绍与实例
Apr 17 Python
python状态机transitions库详解
Jun 02 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/07/10 PHP
php addslashes和mysql_real_escape_string
2010/01/24 PHP
免费的ip数据库淘宝IP地址库简介和PHP调用实例
2014/04/08 PHP
php构造方法中析构方法在继承中的表现
2016/04/12 PHP
Mootools 1.2教程(21)——类(二)
2009/09/15 Javascript
锋利的jQuery jQuery中的DOM操作
2010/03/21 Javascript
js判断字符长度及中英文数字等
2014/03/19 Javascript
使用jquery animate创建平滑滚动效果(可以是到顶部、到底部或指定地方)
2014/05/27 Javascript
javascript如何操作HTML下拉列表标签
2015/08/20 Javascript
Extjs4.0 ComboBox如何实现三级联动
2016/05/11 Javascript
Jquery为DIV添加click事件的简单实例
2016/06/02 Javascript
JavaScript中两个字符串的匹配
2016/06/08 Javascript
用JS实现轮播图效果(二)
2016/06/26 Javascript
JavaScript 详解预编译原理
2017/01/22 Javascript
Angular4学习笔记之根模块与Ng模块
2017/09/09 Javascript
vue中axios解决跨域问题和拦截器的使用方法
2018/03/07 Javascript
详解关于Vuex的action传入多个参数的问题
2019/02/22 Javascript
VUE项目初建和常见问题总结
2019/09/12 Javascript
使用Node.js在深度学习中做图片预处理的方法
2019/09/18 Javascript
Vue 事件的$event参数=事件的值案例
2021/01/29 Vue.js
[01:03:50]DOTA2-DPC中国联赛 正赛 CDEC vs DLG BO3 第二场 2月7日
2021/03/11 DOTA
Windows系统下使用flup搭建Nginx和Python环境的方法
2015/12/25 Python
Python实现递归遍历文件夹并删除文件
2016/04/18 Python
python中pandas.DataFrame排除特定行方法示例
2017/03/12 Python
对Python进行数据分析_关于Package的安装问题
2017/05/22 Python
Python编程实现的简单神经网络算法示例
2018/01/26 Python
Python Excel处理库openpyxl使用详解
2019/05/09 Python
python怎么自定义捕获错误
2020/06/29 Python
详解如何使用rem或viewport进行移动端适配
2020/08/14 HTML / CSS
健康监测猫砂:Pretty Litter
2017/05/25 全球购物
捷克母婴用品购物网站:Feedo.cz
2020/12/28 全球购物
电台实习生求职信
2014/02/25 职场文书
护士求职自荐信范文
2015/03/04 职场文书
校园之声广播稿
2015/08/18 职场文书
导游词之无锡梅园
2019/11/28 职场文书
单机多实例部署 MySQL8.0.20
2022/05/15 MySQL