使用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进制转换(二进制、十进制和十六进制)实例

以下代码用于实现十进制转二进制、八进制、十六进制:

执行以上代码输出结果为:

python3 test.py

输入数字:5

十进制数为:5

转换为二进制为: 0b101

转换为八进制为: 0o5

转换为十六进制为: 0x5

python3 test.py

输入数字:12

十进制数为:12

转换为二进制为: 0b1100

转换为八进制为: 0o14

转换为十六进制为: 0xc

具体实现

十进制到二进制:

十进制到八进制:

十进制到十六进制:

python进2、8、16制转换源码

python 十进制整数转换为任意进制(36以内)

这篇文章就结束到这,需要的朋友可以参考一下,希望大家以后多多支持服务器之家。

python 自定义进制转换,Python 内置函数进制转换的用法(十进制转二进制、八进制、十六进制)...相关推荐

  1. Python培训教程:Python有哪些比较重要的内置函数?

    学习Python技术或者参加Python工作的小伙伴们应该都知道,在Python编程语言中会经常出现很多内置函数,很少有人清楚这些函数,但是它的功能是不可小觑的,下面小编就为大家详细介绍一下Pytho ...

  2. python学习之最常用的内置函数

    python学习之最常用的内置函数 Python 内置函数总共有70余个(通常把内置类也统称为内置函数),覆盖面广,功能强大.不过,对于初学者在初级阶段,掌握下面几个函数是当务之急. (1) 控制台输 ...

  3. python常用内置函数乘法_每个 Python 高手都应该知道的内置函数

    每个 Python 高手都应该知道的内置函数 Python 将纳入高中教材,大学 VB 将被 Python 取代, 现在你还对 Python 一无所知吗?去年就被国外一机构预测, 2017 年 pyt ...

  4. Python中10个常用的内置函数

    大家好,我是小张 在 3.8 版本中,Python 解释器共有近 69 个内置函数可供使用,有了它们能极大地提高编码效率, 数量虽然不少,但在日常搬砖中只用到其中一部分,根据使用频率和用法,列出来几个 ...

  5. python十进制转八进制_Python 内置函数进制转换的用法(十进制转二进制、八进制、十六进制)...

    使用Python内置函数:bin().oct().int().hex()可实现进制转换. 先看Python官方文档中对这几个内置函数的描述: bin(x) Convert an integer num ...

  6. python【数据结构与算法】各进制转换-使用内置函数

    文章目录 1 其他进制向十进制之间的转换 2 十进制向其他进制之间的转换 2.1 使用内置函数来转换 2.2 使用字符串格式化来转化 1 其他进制向十进制之间的转换 string1 = '101010 ...

  7. python_way,day3 集合、函数、三元运算、lambda、python的内置函数、字符转换、文件处理...

    python_way,day3 一.集合 二.函数 三.三元运算 四.lambda 五.python的内置函数 六.字符转换 七.文件处理 一.集合: 1.集合的特性: 特性:无序,不重复的序列 如果 ...

  8. python中int的功能_Python内置函数int()高级用法

    int()函数常用来把其他类型转换为整数,例如: >>> int(3.2) 3>>> int(1/3) 0 其实,int是Python内置类型之一,之所以能够当作函 ...

  9. python常用内置函数总结-Python学习教程之常用的内置函数大全

    前言 内置函数,一般都是因为使用比较频繁或是元操作,所以通过内置函数的形式提供出来.在Python中,python给我们提供了很多已经定义好的函数,这里列出常用的内置函数,分享出来供大家参考学习,下面 ...

最新文章

  1. ORB_SLAM2中Tracking线程
  2. 凝思系统分辨率怎么看_机械液压系统的泄漏怎么办,液压系统基本知识,看完你就懂了...
  3. 画架构图的软件_程序员为什么要学会画技术架构图?
  4. Tomcat——启动错误[A web application must be configured as privileged to be able to load it]解决方案
  5. 阿里巴巴400集python教程_递归的练习课程 | Python从入门到精通:高阶篇之十二-阿里云开发者社区...
  6. Android 系统(153)--- M上默认接入点apn显示
  7. wordPress设计网页实践
  8. 有向图的强连通分量--Tarjan算法---代码分析
  9. 第十四章_超参数调整
  10. Web Service初探
  11. 韩国军事网络指挥中心遭到网络攻击
  12. 人工智能培训的过去与现在
  13. 希尔(Hill)密码(C语言)
  14. javaweb mysql毕业生管理系统_javaweb高校毕业生就业管理系统, springmvc+mysql
  15. 移植tas5707功放芯片驱动注意事项
  16. java 发送邮件怎么抄送_javaMail发送qq邮件(二):可发送抄送密送多人,支持附件...
  17. bgfx入门练习2——找出DX,OpenGL驱动切换实现原理
  18. 最近发现谷歌浏览器打开网页速度很慢,比IE都慢
  19. 周末分享 | 2019年最好的演讲:别让任何人打乱你的人生节奏
  20. 阿波罗-17高钛月海玄武岩的化学组成及其成因的探讨

热门文章

  1. 计算机应用平面设计是,计算机应用(平面设计)
  2. shell脚本实现系统安全巡检
  3. python list函数使用总结_python list(函数list用法)
  4. 牛客编程巅峰赛S2第10场 - 青铜白银黄金题解报告
  5. Linux运维面试-04
  6. GNS3思科模拟器:三层交换机技术
  7. macbook服务器用户名,Mac连接服务器及linux使用
  8. 北风网web开发资深讲师李炎恢出品--ASP系列课程从入门到精通
  9. 一种基于分段线性插值的Gamma校正硬件实现
  10. solr 集成web项目后 执行查询时报错Error from server at http://localhost:8080/solr/collection1