SUNDAY, 12 JULY 2026
Guide For School logo Guide For SchoolStudy Guide For Students On Java Programming
Physics | Chemistry | Mathematics
ICSE | ISC | CBSE
Guide For School logo Guide For SchoolICSE and ISC Resources

Hexadecimal to Decimal Conversion Program in Java and Python

21 July 2014

Hexadecimal to decimal conversion program with two Java methods, algorithm, explanation and Python solution for ICSE and ISC students.

Question:

Write a program to input a number in the Hexadecimal number system and convert it into its equivalent number in the Decimal number system.

Hexadecimal uses digits 0 to 9 and letters A to F, where A means 10, B means 11 and F means 15.

INPUT: Enter a Hexa-Decimal number: 1C28 OUTPUT: The number in Decimal System = 7208

Algorithm:

Step 1: Start.

Step 2: Accept the hexadecimal number as a string and convert it to uppercase.

Step 3: Initialize decimal value dec = 0 and power power = 0.

Step 4: Scan the hexadecimal string from right to left.

Step 5: Extract the current character.

Step 6: Convert digits 0 to 9 to values 0 to 9.

Step 7: Convert letters A to F to values 10 to 15.

Step 8: Add digit × 16^power to dec.

Step 9: Increase power by 1 and continue with the previous character.

Step 10: Display dec.

Step 11: Stop.

Explanation:

The hexadecimal number is accepted as a string because it may contain both digits and letters from A to F. Decimal conversion is based on place value. The rightmost hexadecimal digit has power 0, the next digit has power 1, then power 2, and so on. Therefore the program scans the string from right to left.

In the first Java method, each character is converted to its numeric value using character arithmetic. If the character is between '0' and '9', subtracting 48 gives its digit value. If it is between 'A' and 'F', subtracting 55 gives values 10 to 15. Once the digit value d is found, its contribution is calculated as d * 16^power and added to dec.

The second method follows the same place-value logic but finds the digit value differently. It uses the string 0123456789ABCDEF, where the position of each symbol is its decimal value. Thus indexOf('B') gives 11. Both methods then increase power after processing each digit. When all characters have been scanned, dec contains the decimal equivalent.

The important concept here is weighted place value. A hexadecimal digit does not have the same value at every position. For example, B means 11, but if it appears in the second place from the right, it contributes 11 × 16. If it appears in the third place, it contributes 11 × 16². That is why the program keeps a power variable. Each pass of the loop converts one symbol into its value and then multiplies it by the correct power of 16 before adding it to the total.

Java Program:

Method 1: Using Character Values

Java
/**
* The class HexaToDecimal inputs a Hexadecimal number and converts it into its equivalent Decimal number
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.util.Scanner;

class HexaToDecimal
{
    void hex2Dec(String hex)
    {
        int l = hex.length();
        long dec = 0L;
        int power = 0; // Power of 16, starting from the rightmost digit.

        // Scan from right to left because place values start from 16^0.
        for(int i = l - 1; i >= 0; i--)
        {
            char curDig = hex.charAt(i);
            int d = 0;

            // Convert character digit to its numeric value.
            // Digits 0 to 9 are converted using their ASCII difference.
            if(curDig >= '0' && curDig <= '9')
            d = curDig - 48;
            // Letters A to F represent decimal values 10 to 15.
            else if(curDig >= 'A' && curDig <= 'F')
            d = curDig - 55;

            dec = dec + d * (long)Math.pow(16, power); // Add digit value times its place value.
            power++;
        }

        System.out.println("The number in Decimal System = " + dec);
    }

    public static void main(String args[])
    {
        HexaToDecimal ob = new HexaToDecimal();
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a Hexa-Decimal number: ");
        String hex = sc.nextLine().toUpperCase();

        ob.hex2Dec(hex);
    }
}

Method 2: Using Index Of Hexadecimal Symbols

Java
/**
* The class HexaToDecimal2 inputs a Hexadecimal number and converts it into its equivalent Decimal number
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.util.Scanner;

class HexaToDecimal2
{
    void hex2Dec(String hex)
    {
        int l = hex.length();
        long dec = 0L;
        int power = 0;
        String symbols = "0123456789ABCDEF"; // Position of each symbol gives its decimal value.

        for(int i = l - 1; i >= 0; i--)
        {
            char curDig = hex.charAt(i);
            int d = symbols.indexOf(curDig); // Index gives the decimal value of the hex digit.
            dec = dec + d * (long)Math.pow(16, power);
            power++;
        }

        System.out.println("The number in Decimal System = " + dec);
    }

    public static void main(String args[])
    {
        HexaToDecimal2 ob = new HexaToDecimal2();
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a Hexa-Decimal number: ");
        String hex = sc.nextLine().toUpperCase();

        ob.hex2Dec(hex);
    }
}

Equivalent Python Program:

Python
s = input("Enter a hexadecimal number: ").upper()
hex_digits = "0123456789ABCDEF"
decimal = 0
power = 0

# The rightmost digit has power 0, then the power increases by 1.
# Scan the hexadecimal number from right to left.
for i in range(len(s) - 1, -1, -1):
    ch = s[i]
    value = hex_digits.index(ch)  # Find the decimal value of the hexadecimal digit.
    decimal = decimal + value * (16 ** power)
    power = power + 1

print("Decimal Number =", decimal)

Output:

Enter a Hexa-Decimal number: 4DB The number in Decimal System = 1243

Leave a Reply

Your email address will not be published. Comments are reviewed before appearing publicly.

Send a comment or correction

Study smarter

Everything you need for ICSE and ISC Computer

Programs, revision notes, solved papers and practical guidance—organized for quick study.

Browse all resources →