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中isnumeric()方法的使用简介
May 19 Python
用Eclipse写python程序
Feb 10 Python
python检测空间储存剩余大小和指定文件夹内存占用的实例
Jun 11 Python
python查找指定文件夹下所有文件并按修改时间倒序排列的方法
Oct 21 Python
使用Python进行目录的对比方法
Nov 01 Python
Python实现的银行系统模拟程序完整案例
Apr 12 Python
python f-string式格式化听语音流程讲解
Jun 18 Python
Pyqt QImage 与 np array 转换方法
Jun 27 Python
python中bs4.BeautifulSoup的基本用法
Jul 27 Python
django自定义模板标签过程解析
Dec 14 Python
django日志默认打印request请求信息的方法示例
May 17 Python
从python读取sql的实例方法
Jul 21 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 时间设定)使用入门代码
2008/06/05 PHP
phpmyadmin config.inc.php配置示例
2013/08/27 PHP
php中require和require_once的区别说明
2014/02/27 PHP
安装ImageMagick出现error while loading shared libraries的解决方法
2014/09/23 PHP
Yii实现多按钮保存与提交的方法
2014/12/03 PHP
PHP常见的序列化与反序列化操作实例分析
2019/10/28 PHP
使用Microsoft Ajax Minifier减小JavaScript文件大小的方法
2010/04/01 Javascript
Ext 今日学习总结
2010/09/19 Javascript
Javscript调用iframe框架页面中函数的方法
2014/11/01 Javascript
js实现简单的验证码
2015/12/25 Javascript
深入理解JavaScript中Ajax
2016/08/02 Javascript
node.js实现博客小爬虫的实例代码
2016/10/08 Javascript
基于jQuery实现的查看全文功能【实用】
2016/12/11 Javascript
[js高手之路]从原型链开始图解继承到组合继承的产生详解
2017/08/28 Javascript
微信小程序实现左滑修改、删除功能
2020/10/19 Javascript
javascript中一些奇葩的日期换算方法总结
2018/11/14 Javascript
基于JS实现web端录音与播放功能
2019/04/17 Javascript
对TypeScript库进行单元测试的方法
2019/07/18 Javascript
微信小程序—setTimeOut定时器的问题及解决
2019/07/26 Javascript
Vue Router的手写实现方法实现
2020/03/02 Javascript
精读《Vue3.0 Function API》
2020/05/20 Javascript
[00:27]DOTA2荣耀之路2:Patience from zhou!
2018/05/24 DOTA
用python登录Dr.com思路以及代码分享
2014/06/25 Python
Python cookbook(数据结构与算法)根据字段将记录分组操作示例
2018/03/19 Python
python ftplib模块使用代码实例
2019/12/31 Python
你需要学会的8个Python列表技巧
2020/06/24 Python
VSCode 自定义html5模板的实现
2019/12/05 HTML / CSS
GUESS德国官网:美国牛仔服装品牌
2017/02/14 全球购物
javascript实现用户必须勾选协议实例讲解
2021/03/24 Javascript
上班打牌检讨书
2014/02/07 职场文书
生态养殖创业计划书
2014/05/06 职场文书
我的中国梦演讲稿600字
2014/08/19 职场文书
对领导班子的意见和建议
2015/06/08 职场文书
关于k8s环境部署mysql主从的问题
2022/03/13 MySQL
《雀魂PONG☆》4月1日播出 PV角色设定情报
2022/03/20 日漫
ICOM R71E和R72E图文对比解说
2022/04/07 无线电