天天看点

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

参考链接: Python程序将十进制转换为二进制,八进制和十六进制

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) 

 num = int(input("enter a decimal number: "))

print("\nbinary form is ", bin(num))

print("octal form is ", oct(num))

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