Hexadecimal To Decimal Java

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

Hexadecimal To Decimal Java

Here let’s see how to write a java program to convert a hexadecimal to a decimal number.

Also Read – Decimal to Hexadecimal Java Program

In the below java program we are using Integer.parseInt method to convert hexadecimal to decimal number. The syntax is as below,

public static int parseInt(String s,int radix)

Example:

public class FB
{ 
   public static void main(String[] args)
   { 
      String strHex = "b"; 
      int decimal = Integer.parseInt(strHex, 16); 
      System.out.println("Decimal number : " + decimal);
   }
}

Output:

Decimal number : 11


Java program to convert hexadecimal to decimal

Here we are using method to convert hexadecimal to decimal number,

public class HexaToDecimal
{
   public static int convertHexadecimal(String str)
   {
      String strHexa = "0123456789ABCDEF";
      str = str.toUpperCase();
      int value = 0;
      for(int a = 0; a < str.length(); a++)
      {
         char ch = str.charAt(a);
         int d = strHexa.indexOf(ch);
         value = 16 * value + d;
      }
      return value;
   }

   public static void main(String[] args)
   {
      System.out.println("Decimal value of b is : " + convertHexadecimal("b")); 
      System.out.println("Decimal value of d is : " + convertHexadecimal("d")); 
      System.out.println("Decimal value of 151 is : " + convertHexadecimal("151"));
   }
}

Output:

Decimal value of b is : 11
Decimal value of d is : 13
Decimal value of 151 is : 337

Conclusion

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

Related Posts