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


相关推荐

  • 美化包软件_美化桌面的软件

    美化包软件_美化桌面的软件前言在我们进行自动化测试的时候,用例往往是成百上千,执行的时间是几十分钟或者是小时级别。有时,我们在调试那么多用例的时候,不知道执行到什么程度了,而pytest-sugar插件能很好解决我们的痛点。

    2022年7月31日
  • Kali2021修改更新源[通俗易懂]

    Kali2021修改更新源[通俗易懂]本次演示三种环境更换更新源Xfce4版本KDE版本命令行版本更新源#中科大debhttp://mirrors.ustc.edu.cn/kalikali-rollingmainnon-freecontribdeb-srchttp:/…

    2022年5月27日
  • [转][darkbaby]任天堂传——失落的泰坦王朝(下)[通俗易懂]

    [转][darkbaby]任天堂传——失落的泰坦王朝(下)[通俗易懂]即使是日本业界人士也对1999年发生的“口袋妖怪所有权风波”知之甚少,实际上这个事件的结局足以改变游戏产业未来数十年的势力图,山内溥凭借着个人的睿智让任天堂再次渡过了命运的暗礁,而另一颗曾经炙手可热的璀璨明星却从此销声匿迹……   株式会社POKEMON(简称TPC)成立于1998年4月,由任天堂、Creatures、GAMEFREAK三社共同出资组建,该社成立的目的主要是对全球范围的口…

    2022年7月27日
  • cmd命令怎么切换盘符_cmd命令行怎么切换到d盘

    cmd命令怎么切换盘符_cmd命令行怎么切换到d盘用命令行,要从C盘切换到F盘,然后傻傻地输入了cdcd是在同一个盘下切换空间用的盘之间切换应该用:F:盘符名加一个冒号,大小写不限。

    2022年10月3日
  • 【TCP/IP】IP地址的划分及其分类

    【TCP/IP】IP地址的划分及其分类了解Internet中使用的网络层地址,又称IP地址。每个设备都至少需要一个IP地址,其可以作为我们设备的标识,就跟我们的电话号码一样,知道了电话号码就能找到我们,所以每个IP地址都是唯一的,所以在给每台设备分配IP时,会根据一套编号方案进行。IP作用于OSI参考模型中的网络层,在终端通信中作为唯一标识,便于确定数据的传递目标。IP地址分为:IPv4、IPv6大多数用户熟悉并且流行的IP地址是IPv4,其是用点分四组十进制的表示方法展示的,例如165.195.130.107

    2022年6月14日
  • Eclipse中使用SVN教程「建议收藏」

    Eclipse中使用SVN教程「建议收藏」此文章对Myeclipse同样适用。一.在Eclipse里下载Subclipse插件方法一:从EclipseMarketplace里面下载具体操作:打开Eclipse–>Help–>EclipseMarketplace–>在Find中输入subclipse搜索–>找到subclipse点击install方法二:从InstallNewSoftware里下载具体操作:打

    2022年9月26日

发表回复

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

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