# Hexadecimal to Decimal in Python

Hexadecimal to Decimal 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 Decimal to Hexadecimal, 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 hexadecimal to decimal conversion in Python language. We will be given a hexadecimal number and the python program to convert the given hexadecimal number into an equivalent decimal number.

Mathematically,

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 hexadecimal to decimal. 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 hexadecimal number while declaring the variables. Python program to convert hexadecimal to a decimal using while loop and finally, the result will be displayed on the screen.

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

def HexDecimal(num):  #user-defined function
ch = dec = i = 0
length = len(num) - 1

while length >= 0:
if num[length]>='0' and num[length]<='9':
temp = int(num[length])
elif num[length]>='A' and num[length]<='F':
temp = ord(num[length]) - 55
elif num[length]>='a' and num[length]<='f':
temp = ord(num[length]) - 87
else:
ch = 1
break

dec = dec + (temp * (16 ** i))
length = length - 1
i = i+1

return dec

# take inputs
num = input('Enter hexadecimal number: ')

# calling function and display result
print('The decimal value is =', HexDecimal(num))``````

Output for the different input values:-

The decimal value is = 25

The decimal value is = 93

The decimal value is = 299

The decimal value is = 3106211

## Convert Hexadecimal to Decimal using int() function

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

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

def HexDecimal(n):  #user-defined function
return int(n, 16)

# take inputs
num = input('Enter hexadecimal number: ')

# calling function and display result
print('The decimal value is =', HexDecimal(num))``````

Output:-
{"ft_lang_label":"__label__en","ft_lang_prob":0.63045365,"math_prob":0.9984525,"size":3245,"snap":"2023-40-2023-50","text_gpt3_token_len":869,"char_repetition_ratio":0.22277075,"word_repetition_ratio":0.09498208,"special_character_ratio":0.2844376,"punctuation_ratio":0.07692308,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9997534,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-09-23T12:04:50Z\",\"WARC-Record-ID\":\"<urn:uuid:792f8407-54fc-468a-a0ff-0fc8a1421ff0>\",\"Content-Length\":\"115367\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:7f0ae156-ece9-46b3-91ca-2213805bafcf>\",\"WARC-Concurrent-To\":\"<urn:uuid:6c2eb8b9-3838-44d7-940f-4f74b0900da8>\",\"WARC-IP-Address\":\"172.67.145.30\",\"WARC-Target-URI\":\"https://www.knowprogram.com/python/hexadecimal-to-decimal-python/\",\"WARC-Payload-Digest\":\"sha1:QOPCQTOYM2A2B6FY4CCWZVYTY3HMILYM\",\"WARC-Block-Digest\":\"sha1:6TKLOKKKW37IBQA2QAFA4AIHJSCREAJI\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233506480.7_warc_CC-MAIN-20230923094750-20230923124750-00580.warc.gz\"}"}