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


相关推荐

  • 西数移动硬盘无法读取(高枕无忧的成语)

    原标题:让你的秘密高枕无忧WDMyPassport随行版移动硬盘评测现在是一个数据爆炸的时代,游戏厂商所制作的游戏容量越来越大,短视频风靡我们拍出来的短片越来越多,生活工作中的重要资料更是要好好保存。所以移动硬盘是必备的,一款高速、轻薄、大存储的移动硬盘能改变我们的存储生活。这次入手这款WDMyPassport随行版移动硬盘外观设计小巧轻薄,同时读写速度都在100MB/S左右,还有密码保…

    2022年4月11日
    61
  • goland 2021 激活码【在线破解激活】

    goland 2021 激活码【在线破解激活】,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月16日
    38
  • java清除浏览器缓存[通俗易懂]

    java清除浏览器缓存[通俗易懂]方法一:<%response.setHeader(“Pragma”,”No-cache”);response.addHeader(“Cache-Control”,”must-revalidate”);response.addHeader(“Cache-Control”,”no-cache”);response.addHeader(“Cache-Control”,”no-store”);response.setDateHeader(“Expires”,0)…

    2022年7月18日
    37
  • Kong网关安装_kong网关配置

    Kong网关安装_kong网关配置环境 系统:CentOS7x64 虚拟机:VmwareVMwareWorkstationPro15.1.0build-13591040 安装文档参考官网:https://docs.konghq.com/install/centos/?_ga=2.233277657.61846631.1567134300-1983202451.1567134300 配置yum; 方…

    2022年9月10日
    0
  • java实现简单的图书管理系统「建议收藏」

    java实现简单的图书管理系统「建议收藏」一、项目分布Book类:定义了书的一些属性(书名,作者,价格,分类,状态)并且写了属性的get、set方法Library类:写了登录函数(1、普通用户,2、管理员,3、退出系统),Person类:定义的人的属性(姓名,性别,年龄)并且写了属性的get、set方法,定义了一些书。operate接口:操作的接口,操作方法:查询/查阅,删除/借阅,增加/还书,显示书籍列表Root类:继承…

    2022年7月13日
    15
  • mysql下优化表和修复表命令使用说明(REPAIR TABLE和OPTIMIZE TABLE)

    mysql下优化表和修复表命令使用说明(REPAIR TABLE和OPTIMIZE TABLE)

    2021年9月18日
    57

发表回复

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

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