java hexadecimal to decimal – Java Program to Convert Hexadecimal to Decimal

Java hexadecimal to decimal: In the previous article, we have discussed Java Program for Decimal to Binary

In this article we will see how to convert Hexadecimal to Decimal.

Program to convert hexadecimal to decimal

How to convert hexadecimal to decimal in java: So, before going into the program directly, let’s know about decimal and hexadecimal.

Decimal Number :

  • The number system with base 10 is generally called decimal number system .
  • This number system us usually consists of 10 digits i.e. 0,1,2,3,4,5,6,7,8,9
  • This is the popular number system which is used in daily life .
  • Example – (156)10 where “10” represents the base and “156” represents the decimal number.

Hexadecimal  Number :

  • The number system with base 16 is generally called Hexadecimal number system .
  • This number system us usually consists of 16 digits i.e. 0,1,2,3,4,5,6,7,8,9 and A,B,C,D,E,F
  • Example – (19F)16 where “16” represents the base and “19F” represents the octal number.
  • But (18H)16 will be a wrong representation because the digits are possible between 0 to 9 and A to F.

As the base is 16 so We need to multiply the digits with the base value with the power of 16 while converting it to decimal  .

Let’s take a example  say (4AB)16  is a hexadecimal number that we want to convert it to equivalent decimal then it will be as follows .

(4AB)16  = (4 × 16²) + (10 × 16¹) + (11 × 16⁰)

= 4 x 256 + 10 x 16 + 11 x 1

= 1024 + 160 +11

=(1195)10

Let’s see different ways to convert Hexadecimal to Decimal

Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.

Method-1: Java Program for Hexadecimal to Decimal Using inbuilt function

Approach :

  • Take a hexadecimal value as the input.
  • Convert it into its decimal value by using Integer.parseInt(input value , 16) Store it into a variable output .
  • Print the result.

Program :

Hexadecimal to decimal java Let’s see the program to understand it more clearly.

import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
        // creating scanner object 
         Scanner sc = new Scanner(System.in);
        // input a  octal value as a string  through scanner class 
        System.out.println("Enter a hexadecimal  Value : ");
        String input1=sc.next();
        int output = Integer.parseInt(input1, 16);
        System.out.println("Converted  decimal is :"+output);
    }
}
Output :

Enter a hexadecimal  Value : 4AB
Converted  decimal is : 1195

Method-2 : Java Program for Hexadecimal to Decimal Using switch case

Approach :

  • Take a hexadecimal value form the input.
  • Take each possible value with the help of switch case calculate its decimal equivalent of each digits.
  • Add it each time and Store it into a variable lets say decimal.
  • print the result.

Program :

import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
        // creating scanner object 
        Scanner sc = new Scanner(System.in);
        // input a  octal value as a string  through scanner class 
        System.out.println("Enter a hexadecimal  Value : ");
        String input1=sc.next();
        int len = input1.length() - 1;
        int decimal=0;
    // finding the decimal equivalent of the hexa decimal number
    for(int a = 0; a < input1.length() ; a ++ )
    {
        char ch = input1.charAt(a);
        switch (ch)
        {
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                decimal = decimal + Integer.parseInt(Character.toString(ch))*
                                                (int)Math.pow(16,len);
                len--;
                break;
            case 'a':
            case 'A':
                decimal = decimal + 10 * (int)Math.pow(16,len);
                len--;
                break;
            case 'b':
            case 'B':
                decimal = decimal + 11 * (int)Math.pow(16,len);
                len--;
                break;
            case 'c':
            case 'C':
                decimal = decimal + 12 * (int)Math.pow(16,len);
                len--;
                break;
            case 'd':
            case 'D':
                decimal = decimal + 13 * (int)Math.pow(16,len);
                len--;
                break;
            case 'e':
            case 'E':
                decimal = decimal + 14 * (int)Math.pow(16,len);
                len--;
                break;
            case 'f':
            case 'F':
                decimal = decimal + 15 * (int)Math.pow(16,len);
                len--;
                break;
            default:
                System.out.println("Invalid input");
                break;
        }
    }
        System.out.println("Converted  decimal is :"+decimal);
    }
}

Output :

Enter a hexadecimal  Value : 4AB
Converted  decimal is : 1195

Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.

Related Java Programs: