# Decimal to Hexadecimal in Python

Decimal to Hexadecimal in Python | The hexadecimal number system is a numeral system made up of 16 symbols. The hexadecimal number system is also known as hex. This system uses the decimal numbers (base 10) and six extra symbols (A to F). In hexadecimal no numerical symbols that represent If values greater than nine. This uses the English alphabet like Hexadecimal A = Decimal 10, and Hexadecimal F = Decimal 15. We can also convert from Binary to Decimal and Decimal to Binary, Decimal to Octal and Octal to Decimal, Octal to Binary and Binary to Octal also can be done.

In this post, we will write the programs for decimal to hexadecimal conversion in Python language. We will be given a decimal number and the python program to convert the given decimal number into an equivalent hexadecimal number.

Mathematically,

Hexadecimal number = 3E8
Decimal number = 3 x 162 + 14 x 161 + 8 x 160
= 768 + 224 + 8 = 1000

## Python Program to convert Decimal to Hexadecimal using While loop

This python program using a while loop to convert decimal to hexadecimal. We can also take the help of a user-defined function. A function is a block of code that performs a specific task. We will take a decimal number while declaring the variables. Python program to convert decimal to hexadecimal using while loop and finally, the result will be displayed on the screen.

``````# Python program to convert decimal to hexadecimal

def DecimalHexa(n):  #user-defined function
hexa = ['0'] * 100
i = 0
while(n != 0):

rem = 0
rem = n % 16

if(rem < 10):
hexa[i] = chr(rem + 48)
i = i + 1
else:
hexa[i] = chr(rem + 55)
i = i + 1
n = int(n / 16)

#print hexa number array in reverse order
j = i - 1
while(j >= 0):
print((hexa[j]), end = '');
j = j - 1

# take inputs
num = int(input('Enter a decimal number: '))

# calling function and display result
print('HexaDecimal value = ', end = '')
DecimalHexa(num)``````

Output for the different input values:-

Enter a decimal number: 25
HexaDecimal value = 19

Enter a decimal number: 125
HexaDecimal value = 7D

Enter a decimal number: 4563231
HexaDecimal value = 45A11F

## Convert Decimal to Hexa-decimal using hex() function

This is the simplest and easiest program in python because this program used a built-in function. We will take the decimal number when declaring the variable and print the hexadecimal value of the number using the hex() function.

hex() method returns the hexadecimal form of a passed number as a parameter. It returns hexadecimal numbers in the form of 0xab, where ab is the number form of hexadecimal representation.

``````# Python program to convert decimal to hexadecimal

# take inputs
num = int(input('Enter a decimal number: '))

# display result
print('HexaDecimal value = ', hex(num))``````

Output:-

Enter a decimal number: 50
HexaDecimal value = 0x32

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!