Latest web development tutorials

Python Decimal turn binary, octal, hexadecimal

Document Object Reference Examples Python3

The following code is used to implement decimal turn binary, octal, hexadecimal:

# -*- coding: UTF-8 -*-

# Filename : test.py
# author by : www.w3big.com

# 获取用户输入十进制数
dec = int(input("输入数字:"))

print("十进制数为:", dec)
print("转换为二进制为:", bin(dec))
print("转换为八进制为:", oct(dec))
print("转换为十六进制为:", hex(dec))

Execute the above code output results:

python3 test.py 
输入数字:5
十进制数为:5
转换为二进制为: 0b101
转换为八进制为: 0o5
转换为十六进制为: 0x5
python3 test.py 
输入数字:12
十进制数为:12
转换为二进制为: 0b1100
转换为八进制为: 0o14
转换为十六进制为: 0xc

Document Object Reference Examples Python3