Python程序将十进制转换为二进制,八进制和十六进制

Myth丶恋晨 2022-12-07 13:57 233阅读 0赞

Here you will get python program to convert decimal to binary, octal and hexadecimal.

在这里,您将获得将十进制转换为二进制,八进制和十六进制的python程序。

Python provides inbuilt functions for conversion of one number system to another.

Python提供了将一个数字系统转换为另一个数字系统的内置函数。

  • Convert decimal to binary using bin() function. In converted binary form 0b is present at the beginning.

    使用bin()函数将十进制转换为二进制。 转换后的二进制格式0b 出现在开始时。

  • Convert decimal to octal using oct() function. In converted octal form 0o is present at the beginning.

    使用oct()函数将十进制转换为八进制。 转换八进制形式0o 出现在开始时。

  • Convert decimal to hexadecimal using hex() function. In converted hexadecimal form 0x is present at the beginning.

    使用hex()函数将十进制转换为十六进制 。 转换后的十六进制形式0x 出现在开始时。

Below program demonstrates that how we can do these conversions.

下面的程序演示了我们如何进行这些转换。

Python程序将十进制转换为二进制,八进制和十六进制 (Python Program to Convert Decimal to Binary, Octal and Hexadecimal)

  1. num = int(input("enter a decimal number: "))
  2. print("\nbinary form is ", bin(num))
  3. print("octal form is ", oct(num))
  4. print("hexadecimal form is ", hex(num))

Output

输出量

enter a decimal number: 11

输入十进制数字:11

binary form is 0b1011 octal form is 0o13 hexadecimal form is 0xb

二进制形式为0b1011 八进制形式为0o13 十六进制形式为0xb

Comment below if you have any queries regarding above program.

如果您对以上程序有任何疑问,请在下面评论。

翻译自: https://www.thecrazyprogrammer.com/2017/05/python-program-convert-decimal-binary-octal-hexadecimal.html

发表评论

表情:
评论列表 (有 0 条评论,233人围观)

还没有评论,来说两句吧...

相关阅读