使用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. easyUI之表单

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...

  2. js对象和jQuery对象相互转换

    (1)什么是js对象及代码规则 就是使用js-API,即Node接口中的API或是传统JS语法定义的对象,叫做js对象 js代码规则----divElement var divElement = do ...

  3. python设计模式-命令模式

    命令模式就是对命令的封装.所谓封装命令,就是将一系列操作封装到命令类中,并且命令类只需要对外公开一个执行方法execute,调用此命令的对象只需要执行命令的execute方法就可以完成所有的操作.这样 ...

  4. syslog+rsyslog+logstash+elasticsearch+kibana搭建日志收集

    最近rancher平台上docker日志收集捣腾挺久的,尤其在配置上,特写下记录 Unix/Linux系统中的大部分日志都是通过一种叫做syslog的机制产生和维护的.syslog是一种标准的协议,分 ...

  5. RTSP协议-中文定义

    RTSP协议-中文定义 转自:http://blog.csdn.net/arau_sh/article/details/2982914 E-mail:bryanj@163.com 译者: Bryan. ...

  6. python装饰器的构建

    #!/usr/bin/python3# -*-coding:utf-8 -*-# @Time : 2019/9/27 17:04# @Author : v_ctaozhang import funct ...

  7. JS&和&&-T

    &&逻辑与 &按位与(转换为二进制运算) console.log(1&2); console.log(1&&2); 上面打印的结果是什么呢? 先复习一下 ...

  8. 刀塔OMG塔防1.23单机版使用方法

    使用方法1.确保魔兽的版本为1.26(低版本会报错),如果版本不对用附件中的版本转换器转一下2.把 omg.w3x 复制到 魔兽的MAPS文件夹 WarcraftIII 游戏根目录\Maps3.双击运 ...

  9. Leetcode之70. Climbing Stairs Easy

    Leetcode 70 Climbing Stairs Easy https://leetcode.com/problems/climbing-stairs/ You are climbing a s ...

  10. ZOJ Problem Set - 1005

    注意,条件:B>=C .应考虑B=C的情况. #include<iostream> using namespace std; int A,B,C; void jugs(int a,i ...