Java Program To Convert Hexadecimal To Decimal

Sharing is healthy !!

Learn java program to convert hexadecimal to decimal.

Basically to convert a hexadecimal number to decimal number using manual calculation is quite hard.

So in the java program below we are leveraging two methods namely get_Value() and to_Convert() to do the conversion.

The java program accepts value from the user to do the conversion from hexadecimal to decimal.

So let’s get into example,

Example: Java Program To Convert Hexadecimal To Decimal

Also Read – Decimal to Hexadecimal Java Program

import java.util.Scanner;

public class HexaToDecimal
{
   int number;

   void getValue()
   {
      System.out.println("Java Program To Convert Hexadecimal To Decimal");
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter hexadecimal to convert: ");
      number = Integer.parseInt(sc.nextLine(), 16);
      sc.close();
   }

   void toConvert()
   {
      String decimal = Integer.toString(number);
      System.out.println("The Decimal value is : " + decimal);
   }

   public static void main(String[] args)
   {
      HexaToDecimal htd = new HexaToDecimal();
      htd.getValue();
      htd.toConvert();
   }
}

Output:

Java Program To Convert Hexadecimal To Decimal
Please enter hexadecimal to convert: E5
The decimal value is: 229


using method

import java.util.Scanner;

public class HexaToDecimal
{
   public static int convertHexadecimal(String str)
   {
      String strHexa = "0123456789ABCDEF";
      str = str.toUpperCase();
      int temp = 0;
      for(int a = 0; a < str.length(); a++)
      {
         char h = str.charAt(a);
         int d = strHexa.indexOf(h);
         temp = 16 * temp + d;
      }
      return temp;
   }
 
   public static void main(String[] args)
   {
      String strHexdecimal;
      int decimalNumber;
      Scanner sc = new Scanner(System.in);
      System.out.print("Please enter hexadecimal number : ");
      strHexdecimal = sc.nextLine();
      decimalNumber = convertHexadecimal(strHexdecimal);
      System.out.print("The decimal number is " + decimalNumber);
      sc.close();
   }
}

Output:

Please enter hexadecimal number : e5
The decimal number is 229


using Integer.parseInt() method

import java.util.Scanner;

public class HexaToDecimalExample
{
   public static void main(String[] args)
   {
      String strHexadecimal;
      System.out.print("Please enter hexadecimal number : "); 
      Scanner sc = new Scanner(System.in);
      strHexadecimal = sc.nextLine();
      try
      { 
         Integer decimalNumber = Integer.parseInt(strHexadecimal, 16);
         System.out.println("The decimal number is : " + decimalNumber);
      }
      catch(NumberFormatException nfe)
      { 
         System.out.println("Please enter valid input!!!");
      }
      finally
      {
         sc.close();
      }
   }
}

Output:

Please enter hexadecimal number : e6
The decimal number is : 230


conclusion

That’s it guys. This is all about java hexadecimal to decimal. I hope you have understood the concept.

You can subscribe to my blog flower brackets if you haven’t already.

Do share this article if you like.

Sharing is healthy !!