Python 内置函数进制转换的用法(十进制转二进制、八进制、十六进制)

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前缀。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/119551.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • ValidateRequest

    ValidateRequestASP.Net1.1后引入了对提交表单自动检查是否存在XSS(跨站脚本攻击)的能力。当用户试图用之类的输入影响页面返回结果的时候,ASP.Net的引擎会引发一个HttpRequestValidationExceptioin。默认情况下会返回如下文字的页面:   这是ASP.Net提供的一个很重要的安全特性。因为很多程序员对安全没有概念,甚至都不知道XSS这种攻击的存在,知道主动

    2022年6月10日
    24
  • miniconda可以运行python吗_pycharm怎么配置anaconda环境

    miniconda可以运行python吗_pycharm怎么配置anaconda环境一,Pytorch环境安装打开已经装好的miniconda,用condaenvlist命令查看有哪些已经配置好的环境。如图所示,目前显示有两个环境。创建虚拟环境condacreate-n环境名字(英文)python=x.x(python版本)如:condacreate-npytorchpython=3.7用命令condaactivate虚拟环境名称,进入环境。安装pytorch环境,可以在官网查看相应的命令。这里选用的是cpu版本,命令如下:condain

    2022年8月27日
    3
  • mac idea2021激活码【2021最新】

    (mac idea2021激活码)JetBrains旗下有多款编译器工具(如:IntelliJ、WebStorm、PyCharm等)在各编程领域几乎都占据了垄断地位。建立在开源IntelliJ平台之上,过去15年以来,JetBrains一直在不断发展和完善这个平台。这个平台可以针对您的开发工作流进行微调并且能够提供…

    2022年3月30日
    47
  • Python不使用int()函数把字符串转换为数字「建议收藏」

    Python不使用int()函数把字符串转换为数字「建议收藏」Python不使用int()函数把字符串转换为数字2018年05月21日14:18:45边缘ob边缘ob阅读数:1035https://blog.csdn.net/qq_33192555/a

    2022年7月6日
    26
  • phantomjs环境搭建已经运行

    phantomjs环境搭建已经运行

    2021年12月6日
    42
  • python 3.6安装cPickle

    python 3.6安装cPicklepython36找不到pickle这个包,直接使用import_pickleascPickle即可,亲测可用https://blog.csdn.net/bailixuance/article/details/850544591、在python2.X中,需要安装cPickle,2、在python3.X中,这个包已被别的包替换,使用以下语句即可:import_picklea…

    2022年6月16日
    118

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号