Hexadecimal To Decimal Java

Hey guys!! Welcome to flower brackets blog. In this post we are going to learn hexadecimal to decimal java.

Here let’s see how to go from hexadecimal to decimal. Basically to convert a hexadecimal to decimal java using manual calculation is quite hard.

Also Read – Decimal to Hexadecimal Java Program

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 convert hexadecimal to decimal java,

Example: hexadecimal to decimal java

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

Now let’s see hex string to decimal java using an example,

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

Let’s see how to convert hex to decimal java using 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


Using bufferedreader

Now let’s see hexadecimal to decimal java using bufferedreader class,

import java.io.*;

public class HexaToDecimalDemo
{
   public static void main(String[] args) throws IOException
   {
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      System.out.println("Please enter hexadecimal to convert : ");
      String strHexa = br.readLine();
      int a = Integer.parseInt(strHexa, 16);
      System.out.println("The decimal value is : " + a);
   }
}

Output:

Please enter hexadecimal to convert : e1
The decimal value is : 225


conclusion

That’s it guys. This is all about how to convert hexadecimal to decimal in java. 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.

You May Also Like