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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • svn用户名和密码设置_git配置本地用户名密码

    svn用户名和密码设置_git配置本地用户名密码SVN配置用户192.168.1.200   Administrator     ydd!@#$1234()90 1、备份D:\svn\conf\authz,再修改2、备份D:\svn\conf\passwd,再修改注意:* 权限配置文件中出现的用户名必须已在用户配置文件中定义。* 对权限配置文件的修改立即生效,不必重启s

    2022年9月13日
    1
  • 关闭eslint检测[通俗易懂]

    关闭eslint检测[通俗易懂]bulid>webpack.base.config.js将createLintingRule方法内容注释转载文章关闭eslint检测

    2022年5月1日
    52
  • VSCode 断点调试项目「建议收藏」

    VSCode 断点调试项目「建议收藏」1.安装必须程序首页下载VSCode,打开一个项目扩展安装DebuggerforChrome插件用于Chrome调试;点击扩展搜索DebuggerforChrome然后点击安装2.VScode项目配置打开项目界面按F5出现界面选择Chrome添加成功后点击调试,添加配置,自动生成launch.json,添加配置的url为iis配置地址3…

    2022年5月21日
    38
  • TCP和UDP的特点_TCP和UDP位于

    TCP和UDP的特点_TCP和UDP位于原文链接:https://www.jianshu.com/p/ef1811b3b44eOSI参考模型和TCP/IP协议群1.TCP/IP协议群的具体含义从字面意义上讲,有人可能会认为TCP/IP是指TCP和IP两种协议.实际生活中有时也确实就是指这两种协议.然而在很多情况下,它只是利用IP进行通信时所必须用到的协议群的统称.具体来说,IP或ICMP,TCP或U…

    2022年9月20日
    4
  • PhpStorm 头部注释、类注释和函数注释的设置

    PhpStorm 头部注释、类注释和函数注释的设置

    2021年11月8日
    38
  • printwriter用法_stylewriter使用教程

    printwriter用法_stylewriter使用教程OutPutStream可以被封装成PrintWriter,OutputStream比较底层一些,是以字节为单位传输的,而PrintWriter是以字符为单位输出,所以就会涉及到转码编码的问题,如果用PrintWriter发送char[]和byte[],在服务端收到的常常是有一些字符会失真.PrintWriter以字符为单位,支持汉字,OutPutStream则不行总结:处理

    2022年8月10日
    4

发表回复

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

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