In the previous article, we have discussed Python Program to Convert Octal to Binary
Given a hexadecimal number and the task is to get the respective decimal number.
Examples:
Example1:
Input:
Given Hexadecimal String = "2C"
Output:
The Decimal value of the given Hexadecimal number { 2C } is: 44
Example2:
Input:
Given Hexadecimal String = "6AB"
Output:
The Decimal value of the given Hexadecimal number { 6AB } is: 1707
Program to Convert Hexadecimal to Decimal in Python
Below are the ways to convert the given hexadecimal number into decimal :
Method #1: Using While Loop (Static Input)
Approach:
- Import math module using the import keyword.
- Give the hexadecimal string as static input and store it in a variable.
- Take a variable say ‘rslt_val ‘and initialize its value with 0.
- Take another variable say deciml_num and initialize its value with 0.
- Calculate the length of the given hexadecimal number and store it in a variable k.
- Decrement the value of k by 1 and store it in the same variable k.
- Loop from 0 to the length of the given hexadecimal number using the for loop.
- Inside the loop, check if the iterator value of the hexadecimal string is greater than or equal to 0 and less than or equal to 9 using the if conditional statement.
- If it is true, then convert the iterator value of the hexadecimal string into an integer using the int() function.
- Assign its value to the above-initialized rslt_val.
- Check if the iterator value of the hexadecimal string is equal to ‘A’ or ‘a’ using the if conditional statement.
- If it is true, then assign value 10 to the above-initialized rslt_val.
- Check if the iterator value of the hexadecimal string is equal to ‘B’ or ‘b’ using the if conditional statement.
- If it is true, then assign value 11 to the above-initialized rslt_val.
- Check if the iterator value of the hexadecimal string is equal to ‘C’ or ‘c’ using the if conditional statement.
- If it is true, then assign value 12 to the above-initialized rslt_val.
- Check if the iterator value of the hexadecimal string is equal to ‘D’ or ‘d’ using the if conditional statement.
- If it is true, then assign value 13 to the above-initialized rslt_val.
- Check if the iterator value of the hexadecimal string is equal to ‘E’ or ‘e’ using the if conditional statement.
- If it is true, then assign value 14 to the above-initialized rslt_val.
- Check if the iterator value of the hexadecimal string is equal to ‘F’ or ‘f’ using the if conditional statement.
- If it is true, then assign value 15 to the above-initialized rslt_val.
- Multiply rslt_val with 16 raised to the power k using math.pow() function and convert it into integer using the int() function.
- Add this with the deciml_num and store it in the same variable deciml_num.
- Decrease the value of k by 1 and store it in the same variable k.
- Print the deciml_num to get the decimal value of the given hexadecimal number.
- The Exit of the Program.
Below is the implementation:
# Import math module using the import keyword. import math # Give the hexadecimal string as static input and store it in a variable. hexa_num = "2C" # Take a variable say 'rslt_val 'and initialize its value with 0. rslt_val = 0 # Take another variable say deciml_num and initialize its value with 0. deciml_num = 0 # Calculate the length of the given hexadecimal number and store it in a variable k. k = len(hexa_num) # Decrement the value of k by 1 and store it in the same variable k. k -= 1 print( "The Decimal value of the given Hexadecimal number {", hexa_num, "} is: ") # Loop from 0 to the length of the given hexadecimal number using the for loop. for itr in range(0, len(hexa_num)): # Inside the loop, check if the iterator value of the hexadecimal string is greater than 0 # and less than 9 using the if conditional statement. if hexa_num[itr] >= '0' and hexa_num[itr] <= '9': # If it is true, then convert the iterator value of the hexadecimal string into an integer # using the int() function. # Assign its value to the above-initialized rslt_val. rslt_val = (int)(hexa_num[itr]) # Check if the iterator value of the hexadecimal string is equal to 'A' or 'a' using the # if conditional statement. # If it is true, then assign value 10 to the above-initialized rslt_val. if hexa_num[itr] == 'A' or hexa_num[itr] == 'a': rslt_val = 10 # Check if the iterator value of the hexadecimal string is equal to 'B' or 'b' using the # if conditional statement. # If it is true, then assign value 11 to the above-initialized rslt_val. if hexa_num[itr] == 'B' or hexa_num[itr] == 'b': rslt_val = 11 # Check if the iterator value of the hexadecimal string is equal to 'C' or 'c' using the # if conditional statement. # If it is true, then assign value 12 to the above-initialized rslt_val. if hexa_num[itr] == 'C' or hexa_num[itr] == 'c': rslt_val = 12 # Check if the iterator value of the hexadecimal string is equal to 'D' or 'd' using the # if conditional statement. # If it is true, then assign value 13 to the above-initialized rslt_val. if hexa_num[itr] == 'D' or hexa_num[itr] == 'd': rslt_val = 13 # Check if the iterator value of the hexadecimal string is equal to 'E' or 'e' using the # if conditional statement. # If it is true, then assign value 14 to the above-initialized rslt_val. if hexa_num[itr] == 'E' or hexa_num[itr] == 'e': rslt_val = 14 # Check if the iterator value of the hexadecimal string is equal to 'F' or 'f' using the # if conditional statement. # If it is true, then assign value 15 to the above-initialized rslt_val. if hexa_num[itr] == 'F' or hexa_num[itr] == 'f': rslt_val = 15 # Multiply rslt_val with 16 raised to the power k using math.pow() function and convert it # into integer using the int() function. # Add this with the deciml_num and store it in the same variable deciml_num. deciml_num = deciml_num+(int)(rslt_val*math.pow(16, k)) # Decrease the value of k by 1 and store it in the same variable k. k -= 1 # Print the deciml_num to get the decimal value of the given hexadecimal number. print(deciml_num)
Output:
The Decimal value of the given Hexadecimal number { 2C } is: 44
Method #2: Using While loop (User Input)
Approach:
- Import math module using the import keyword.
- Give the hexadecimal string as user input using the input() function and store it in a variable.
- Take a variable say ‘rslt_val ‘and initialize its value with 0.
- Take another variable say deciml_num and initialize its value with 0.
- Calculate the length of the given hexadecimal number and store it in a variable k.
- Decrement the value of k by 1 and store it in the same variable k.
- Loop from 0 to the length of the given hexadecimal number using the for loop.
- Inside the loop, check if the iterator value of the hexadecimal string is greater than 0 and less than 9 using the if conditional statement.
- If it is true, then convert the iterator value of the hexadecimal string into an integer using the int() function.
- Assign its value to the above-initialized rslt_val.
- Check if the iterator value of the hexadecimal string is equal to ‘A’ or ‘a’ using the if conditional statement.
- If it is true, then assign value 10 to the above-initialized rslt_val.
- Check if the iterator value of the hexadecimal string is equal to ‘B’ or ‘b’ using the if conditional statement.
- If it is true, then assign value 11 to the above-initialized rslt_val.
- Check if the iterator value of the hexadecimal string is equal to ‘C’ or ‘c’ using the if conditional statement.
- If it is true, then assign value 12 to the above-initialized rslt_val.
- Check if the iterator value of the hexadecimal string is equal to ‘D’ or ‘d’ using the if conditional statement.
- If it is true, then assign value 13 to the above-initialized rslt_val.
- Check if the iterator value of the hexadecimal string is equal to ‘E’ or ‘e’ using the if conditional statement.
- If it is true, then assign value 14 to the above-initialized rslt_val.
- Check if the iterator value of the hexadecimal string is equal to ‘F’ or ‘f’ using the if conditional statement.
- If it is true, then assign value 15 to the above-initialized rslt_val.
- Multiply rslt_val with 16 raised to the power k using math.pow() function and convert it into integer using the int() function.
- Add this with the deciml_num and store it in the same variable deciml_num.
- Decrease the value of k by 1 and store it in the same variable k.
- Print the deciml_num to get the decimal value of the given hexadecimal number.
- The Exit of the Program.
Below is the implementation:
# Import math module using the import keyword. import math # Give the hexadecimal string as user input using the input() function and # store it in a variable. hexa_num = input("Enter some random String Number = ") # Take a variable say 'rslt_val 'and initialize its value with 0. rslt_val = 0 # Take another variable say deciml_num and initialize its value with 0. deciml_num = 0 # Calculate the length of the given hexadecimal number and store it in a variable k. k = len(hexa_num) # Decrement the value of k by 1 and store it in the same variable k. k -= 1 print( "The Decimal value of the given Hexadecimal number {", hexa_num, "} is: ") # Loop from 0 to the length of the given hexadecimal number using the for loop. for itr in range(0, len(hexa_num)): # Inside the loop, check if the iterator value of the hexadecimal string is greater than 0 # and less than 9 using the if conditional statement. if hexa_num[itr] >= '0' and hexa_num[itr] <= '9': # If it is true, then convert the iterator value of the hexadecimal string into an integer # using the int() function. # Assign its value to the above-initialized rslt_val. rslt_val = (int)(hexa_num[itr]) # Check if the iterator value of the hexadecimal string is equal to 'A' or 'a' using the # if conditional statement. # If it is true, then assign value 10 to the above-initialized rslt_val. if hexa_num[itr] == 'A' or hexa_num[itr] == 'a': rslt_val = 10 # Check if the iterator value of the hexadecimal string is equal to 'B' or 'b' using the # if conditional statement. # If it is true, then assign value 11 to the above-initialized rslt_val. if hexa_num[itr] == 'B' or hexa_num[itr] == 'b': rslt_val = 11 # Check if the iterator value of the hexadecimal string is equal to 'C' or 'c' using the # if conditional statement. # If it is true, then assign value 12 to the above-initialized rslt_val. if hexa_num[itr] == 'C' or hexa_num[itr] == 'c': rslt_val = 12 # Check if the iterator value of the hexadecimal string is equal to 'D' or 'd' using the # if conditional statement. # If it is true, then assign value 13 to the above-initialized rslt_val. if hexa_num[itr] == 'D' or hexa_num[itr] == 'd': rslt_val = 13 # Check if the iterator value of the hexadecimal string is equal to 'E' or 'e' using the # if conditional statement. # If it is true, then assign value 14 to the above-initialized rslt_val. if hexa_num[itr] == 'E' or hexa_num[itr] == 'e': rslt_val = 14 # Check if the iterator value of the hexadecimal string is equal to 'F' or 'f' using the # if conditional statement. # If it is true, then assign value 15 to the above-initialized rslt_val. if hexa_num[itr] == 'F' or hexa_num[itr] == 'f': rslt_val = 15 # Multiply rslt_val with 16 raised to the power k using math.pow() function and convert it # into integer using the int() function. # Add this with the deciml_num and store it in the same variable deciml_num. deciml_num = deciml_num+(int)(rslt_val*math.pow(16, k)) # Decrease the value of k by 1 and store it in the same variable k. k -= 1 # Print the deciml_num to get the decimal value of the given hexadecimal number. print(deciml_num)
Output:
Enter some random String Number = 6AB The Decimal value of the given Hexadecimal number { 6AB } is: 1707
Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.