python中进制转换
使用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中进制转换的更多相关文章
- Python中进制转换函数的使用
Python中进制转换函数的使用 关于Python中几个进制转换的函数使用方法,做一个简单的使用方法的介绍,我们常用的进制转换函数常用的就是int()(其他进制转换到十进制).bin()(十进制转换到 ...
- python 中进制转换及format(),int()函数用法
python中数值型变量好像只能是十进制形式表示,其他类型变量只能以字符串形式存在,可以通过format函数将int类型变量转换成其他进制字符串,如下所示: v_code=15 # 2进制 x=for ...
- 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 ...
- python任意进制转换
python任意进制转换 import string def module_n_converter(q, s, base=None): """ 将自然数按照给定的字符串转 ...
- python实现进制转换(二、八、十六进制;十进制)
python实现进制转换(二.八.十六进制:十进制) (一)十进制整数转为二.八.十六进制 1.format实现转换>>> format(2,"b") # (10 ...
- python中进制之间的转换
参考于:http://www.360doc.com/content/14/0428/11/16044571_372866302.shtml 在此非常感谢! ~~~~~~~~~~~~~~~~~~~~~ ...
- C++中进制转换问题
一直在刷题的时候,都会遇到一个坑,就是进制转换的问题.而每一次都傻乎乎的自己去实现一个.所以算是对以前的坑的一个总结. itoa 函数 itoa是广泛应用的非标准C语言和C++语言扩展函数.由于它不是 ...
- Python 各种进制转换
#coding=gbk var=input("请输入十六进制数:") b=bin(int(var,16)) print(b[2:]) 详细请参考python自带int函数.bin函 ...
- python之进制转换
Python中二进制是以0b开头的: 例如: 0b11 则表示十进制的3 8进制是以0开头的: 例如: 011则表示十进制的9 16进制是以0x开头的: 例如: 0x11则表示十进制 ...
随机推荐
- PhoneUtils
import java.util.regex.Matcher; import java.util.regex.Pattern; public class PhoneUtils { /** * @par ...
- JVM源码分析之JDK8下的僵尸(无法回收)类加载器[z]
[z]http://lovestblog.cn/blog/2016/04/24/classloader-unload/ 概述 这篇文章基于最近在排查的一个问题,花了我们团队不少时间来排查这个问题,现象 ...
- NLP之电影评分数据的情感分析
1.基于词袋模型的逻辑回归情感分类 # coding: utf-8 import re import numpy as np import pandas as pd from bs4 import B ...
- Trie代码学习
感觉不把这个Trie理解一下,AC自动机的代码看起来有点费劲. 这里代码的学习仿照训练指南209页. 这里如果只是查询单词,感觉用map更好,但是如果查前缀,还是用Trie. 1.Trie查询前缀字符 ...
- 【笔记】Docker入门
这个文章讲的比较透彻,就不复制粘贴了 <Docker从入门到实践>阅读笔记 Docker安装 环境 root@fudonghai:~# uname -a Linux fudonghai - ...
- springboot-springmvc
0依赖 <!-- jsp --> <dependency> <groupId>org.apache.tomcat.embed</groupId> < ...
- 日常小节----unity小坑记(静态后不可移动和旋转)
当物体被置为静态时,模型网格是无法移动和旋转的,只有碰撞器可以. 也就是会出现当父物体不为静态,子物体为静态时,运行后旋转移动父物体,子物体模型网格不会跟随旋转移动,但子物体碰撞器会跟随旋转移动. 或 ...
- 安装与编译Dlib(以Ubuntu16.04+Python3.6+pip为例)
安装与编译Dlib(以Ubuntu16.04+Python3.6+pip为例) Step1:下载Ubuntu (or Linux)系统支持库=>Install OS libraries -dev ...
- 《Java语言程序设计》第三讲类与对象“动手动脑”
一.以下代码为何无法通过编译?哪儿出错了? 答: 如果类提供了一个自定义的构造方法,将导致系统不再提供默认构造方法. 二. (1)以下代码输出结果是什么? package xy; public cla ...
- Go语言中的打包和工具链
包 所有Go语言的程序都会组织成若干组文件,每组文件被称为一个包.这样每个包的代码都可以作为很小的复用单元,被其他项目引用. 包名惯例 给包命名的惯例是使用包所在目录的名字.并不需要所有包的名字都与别 ...