Python program to convert Decimal to Binary and decimal to hexadecimal
In this post, let's learn how to convert Decimal to Binary and decimal to hexadecimal in Python.
Python provides rich set of in-built functions, and we are going to bin()
function to convert the decimal number to binary and hex()
function to convert the decimal number to hexadecimal.
decimal = int (input("Enter Decimal Value: \n"))
convert = int (input("Convert into: [1] Binary, [2] Hexadecimal: \n"))
if convert == 1:
print("Converted to Binary: ", bin(decimal).replace("0b", ""))
elif convert == 2:
print("Converted to Hexadecimal: ", hex(decimal))
else:
print("Invalid input")
Results
Enter Decimal Value: 79
Convert into: [1] Binary, [2] Hexadecimal: 1
Converted to Binary: 1001111
Enter Decimal Value: 79
Convert into: [1] Binary, [2] Hexadecimal: 2
Converted to Hexadecimal: 0x4f