使用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.

#10进制转为2进制
>>> bin(10)
'0b1010' #2进制转为10进制
>>> int("",2)
9 #10进制转为16进制
>>> hex(10)
'0xa' #16进制到10进制
>>> int('ff', 16)
255 >>> int('0xab', 16)
171 #十进制转为八进制
>>print("%o" % 10)
>>12 #16进制到2进制
>>> bin(0xa)
'0b1010' #10进制到8进制
>>> oct(8)
'' #2进制到16进制
>>> hex(0b1001)
'0x9'

refer:https://www.cnblogs.com/jsplyy/p/5636246.html

python中进制转换的更多相关文章

  1. Python中进制转换函数的使用

    Python中进制转换函数的使用 关于Python中几个进制转换的函数使用方法,做一个简单的使用方法的介绍,我们常用的进制转换函数常用的就是int()(其他进制转换到十进制).bin()(十进制转换到 ...

  2. python 中进制转换及format(),int()函数用法

    python中数值型变量好像只能是十进制形式表示,其他类型变量只能以字符串形式存在,可以通过format函数将int类型变量转换成其他进制字符串,如下所示: v_code=15 # 2进制 x=for ...

  3. python 实现进制转换(二进制转十进制)

    摘自https://baike.baidu.com/item/%E5%8D%81%E8%BF%9B%E5%88%B6%E8%BD%AC%E4%BA%8C%E8%BF%9B%E5%88%B6 pytho ...

  4. python任意进制转换

    python任意进制转换 import string def module_n_converter(q, s, base=None): """ 将自然数按照给定的字符串转 ...

  5. python实现进制转换(二、八、十六进制;十进制)

    python实现进制转换(二.八.十六进制:十进制) (一)十进制整数转为二.八.十六进制 1.format实现转换>>> format(2,"b") # (10 ...

  6. python中进制之间的转换

    参考于:http://www.360doc.com/content/14/0428/11/16044571_372866302.shtml  在此非常感谢! ~~~~~~~~~~~~~~~~~~~~~ ...

  7. C++中进制转换问题

    一直在刷题的时候,都会遇到一个坑,就是进制转换的问题.而每一次都傻乎乎的自己去实现一个.所以算是对以前的坑的一个总结. itoa 函数 itoa是广泛应用的非标准C语言和C++语言扩展函数.由于它不是 ...

  8. Python 各种进制转换

    #coding=gbk var=input("请输入十六进制数:") b=bin(int(var,16)) print(b[2:]) 详细请参考python自带int函数.bin函 ...

  9. python之进制转换

    Python中二进制是以0b开头的:    例如: 0b11 则表示十进制的3 8进制是以0开头的:    例如: 011则表示十进制的9 16进制是以0x开头的:    例如: 0x11则表示十进制 ...

随机推荐

  1. selenium 学习中遇到的问题汇总

    1.使用document.getByClassName时无click事件,然后就不知道怎么办了,也不太懂前端,与开发大哥确认,div 中class实现展开和收起是通过隐藏和显示这种方式实现的,在编写时 ...

  2. 京东商城跨域设置Cookie实现SSO单点登陆过程

    可以先看下这边文章:http://blog.chinaunix.net/uid-25508399-id-3431705.html   1.点击首页的登陆按钮跳转到京东的登陆中心https://pass ...

  3. Delphi编写的等长加密与解密

    最近在看一本关于网络游戏服务端开发的书,虽然该书是个空架子,里面没有多少实际的内容(此书评价不好),但其中关于等长加密与解密的代码还是有一定的借鉴作用的.他山之石,可以攻玉.因为书中是C++的代码,所 ...

  4. CTF—攻防练习之Capture the Flag

    主机:192.168.32.152 靶机:192.168.32.160 首先nmap扫描端口: ftp,ssh,http服务 dirb扫描目录,flag下有一个flag password目录下,查看源 ...

  5. STS中MyBatis的基本实现

    本文采用的是<深浅spring boot 2.x>中第5章的例子,用一个接口实现对一个表项的读取. 数据库:mysql下建立user数据库,表名为t_usr 1. 数据源设置 在appli ...

  6. mdk3 工具使用-表白神器

    一:在root目录下新建个txt文档,用vim编辑文档,写几段暧昧的话 二:终端执行命令: 开启网卡监听模式 airmon-ng start wlan0 开启无线广播 mdk3 wlan0mon b ...

  7. struts框架之环境搭建(一)

    一.首先,我们需要做一些准备工作: 1.1.  下载struts:https://struts.apache.org/download.cgi#struts2520 1.2.  下载tomcat:ht ...

  8. LESSON 7- High Rate Quantizers and Waveform Encoding

    1.      The Lloyd-Max algorithm is hill-climbing algorithm 第六节最后提出一个好的quantizer必须满足Lloyd-Max条件,但满足Ll ...

  9. [转帖]如何在Linux上使用命令行查看硬件信息

    如何在Linux上使用命令行查看硬件信息 时间:2016-01-13   作者:admin 分类:新手入门 阅读:126次 http://embeddedlinux.org.cn/emb-linux/ ...

  10. SQLite基础-3.语法与数据类型

    目录 一.SQLite语法 1. 大小写敏感性 2. 注释 3. SQLite语句 二. SQLite 数据类型 1. Boolean 数据类型 2. Date 与 Time 数据类型 一.SQLit ...