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


相关推荐

  • java线程池拒绝策略_java线程池拒绝策略有哪些?

    java线程池拒绝策略_java线程池拒绝策略有哪些?小伙伴们知道java中线程池拒绝策略有哪些吗?这是java线程池必须知道的基础之一,下面就一起来看看吧。在java线程池中,有着这么四种拒绝策略:1)、AbortPolicy(默认)直接抛出RejectedExecutionException异常阻止系统正常运行。publicstaticclassAbortPolicyimplementsRejectedExecutionHandler{…

    2022年6月17日
    25
  • 寻找最长回文子串

    寻找最长回文子串最长回文子串的问题描述:给出一个字符串S,求S的最长回文子串的长度。样例:字符串”PATZJUJZTACCBCC”的最长回文子串为“ATZJUJZTA”,长度为9。先看暴力解法:枚举子串的两个端点i和j,判断在i,区间内的子串是否回文。从复杂度上来看,枚举端点需要O(n2),判断回文需要O(n),因此总复杂度是O(n3)。介绍动态规划的方法,使用动态规划可以达到…

    2022年5月22日
    40
  • 网络交换机光口和电口_交换机的光口

    网络交换机光口和电口_交换机的光口 一、光口1、基本概念     光口是光纤接口的简称。   也可称之为:G口 (意思是G光纤口)   光口:所应用于机房,机柜等大型设备的一个光纤带宽接口。   光纤可以用于音频(声卡有光输出的),网络(光纤作为传输介质),磁盘(光纤代替电缆传输数据)等等。   光纤又可分为单模光纤和多模光纤区别如下:   单模光纤和多模光纤可以从纤芯的尺…

    2022年10月21日
    2
  • (3)JMeter元件详解之 Include Controlle 包含控制器

    (3)JMeter元件详解之 Include Controlle 包含控制器

    2021年7月13日
    82
  • java volatile关键字的作用_java volatile关键字作用及使用场景详解

    java volatile关键字的作用_java volatile关键字作用及使用场景详解1.volatile关键字的作用:保证了变量的可见性(visibility)。被volatile关键字修饰的变量,如果值发生了变更,其他线程立马可见,避免出现脏读的现象。如以下代码片段,isShutDown被置为true后,doWork方法仍有执行。如用volatile修饰isShutDown变量,可避免此问题。publicclassVolatileTest3{staticclassW…

    2022年5月31日
    35
  • [LeetCode] Top K Frequent Elements 前K个高频元素

    [LeetCode] Top K Frequent Elements 前K个高频元素

    2022年3月12日
    42

发表回复

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

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