Python内置函数bin() oct()等实现进制转换

 更新时间:2012年12月30日 16:27:50   作者:  
使用Python内置函数:bin()、oct()、int()、hex()可实现进制转换;先看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.
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前缀。

相关文章

  • PyTorch搭建多项式回归模型(三)

    PyTorch搭建多项式回归模型(三)

    这篇文章主要为大家详细介绍了PyTorch搭建多项式回归模型,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-05-05
  • Python3读取文件常用方法实例分析

    Python3读取文件常用方法实例分析

    这篇文章主要介绍了Python3读取文件常用方法,以实例形式较为详细的分析了Python一次性读取、逐行读取及读取文件一部分的实现技巧,需要的朋友可以参考下
    2015-05-05
  • pandas获取对应的行或者列方式

    pandas获取对应的行或者列方式

    这篇文章主要介绍了pandas获取对应的行或者列方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-02-02
  • Python如何将控制台输出另存为日志文件

    Python如何将控制台输出另存为日志文件

    这篇文章主要介绍了Python如何将控制台输出另存为日志文件问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-05-05
  • Python datatime库语法使用详解

    Python datatime库语法使用详解

    这篇文章主要介绍了Python datatime库语法使用详解,datetime模块用于是date和time模块的合集,文章围绕相关资料展开详情,感兴趣的小伙伴可以擦参考一下
    2022-07-07
  • Python异步库asyncio、aiohttp详解

    Python异步库asyncio、aiohttp详解

    这篇文章主要介绍了Python异步库asyncio、aiohttp使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-06-06
  • Python中sklearn实现交叉验证示例分析

    Python中sklearn实现交叉验证示例分析

    这篇文章主要介绍了Python中sklearn实现交叉验证,本文python的版本为3.8,各个版本之间函数名字略有不同,但是原理都是一样的,集成开发环境使用的是Anaconda的Spyder,需要的朋友可以参考下
    2023-08-08
  • Django--权限Permissions的例子

    Django--权限Permissions的例子

    今天小编就为大家分享一篇Django--权限Permissions的例子,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-08-08
  • 在Python 2.7即将停止支持时,我们为你带来了一份python 3.x迁移指南

    在Python 2.7即将停止支持时,我们为你带来了一份python 3.x迁移指南

    这篇文章主要介绍了在Python 2.7即将停止支持时我们为你准备了一份python 3.x迁移指南的相关资料,需要的朋友可以参考下
    2018-01-01
  • python粘包的解决方案

    python粘包的解决方案

    粘包就是在数据传输过程中有多个数据包被粘连在一起被发送或接受,本文主要介绍了python粘包的解决方案,具有一定的参考价值,感兴趣的可以了解一下
    2024-01-01

最新评论