Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Java Program for Hexadecimal to Decimal Conversion



The problem statement states that given a hexadecimal number, write a Java program to convert it into its equivalent decimal number system. The base value of hexadecimal is 16 and decimal is 10. When we convert the hexadecimal to decimal, the base value 16 will be changed to 10.

The number system is of four types: Binary, Octal, Decimal and Hexadecimal. The base value depends on the number of digits contained by number system. For example, binary number system contains only two digits 0 and 1. Hence, its base is 2.

Hexadecimal number system

It represents the numbers from 0 to 9 and A to F. There is a total of 16 numbers available in it and its base value is also 16. 12A16, 34B16, 45C16 are a few examples of hexadecimal. In computers, the code for colors is generally written in hexadecimal form.

Suppose, we have to store a large decimal value if we store it in binary numbering system then binary string may become very long. In this situation, we can use hexadecimal number system that can store 4 bits of binary into 1 bit. It shortens the length of bits.

Decimal number system

It is the most used number system. It has 10 digits from 0 to 9. Hence, its base value is 10. If base value of a number is not mentioned then, it is considered to be 10. A single digit has a weight of power 10 and every digit is 10 times more weighty than previous one. For example, 1010, 43110, 98010 etc.

Hexadecimal to Decimal Conversion

Given hexadecimal number is (54A)16. To convert it to decimal, multiply each digit with 16n-1 (where, n is the number of digits) as shown below ?

(54A)16 = 5 * 163-1 + 4 * 162-1 + A * 161-1
		= 5 * 162  + 4 * 161 + 10 * 160  
		= 5 * 256 + 64 + 10 
		= 1280 + 74
		= 1354 

Using Integer.parseInt() Method

It is a static method of Integer class that returns a decimal value according to the specified base. It is available in java.lang package.

Syntax

Integer.parseInt("String", base); 

Where,

  • String ? The value that is going to be converted.

  • Base ? The given value is converted according to the specified base.

Example

In this example, we will use the Integer.parseInt() method to convert hexadecimal to decimal.

public class Conversion {
   public static void main(String args[]) {  
      // Converting and storing hexadecimal value to dec1 and dec2 with base 16  
      int dec1 = Integer.parseInt("54A", 16);
      int dec2 = Integer.parseInt("41C", 16);
      System.out.println("Decimal value of given Hexadecimal: " + dec1);
      System.out.println("Decimal value of given Hexadecimal: " + dec2);
   }
}

On running, this code will show below result ?

Decimal value of given Hexadecimal: 1354
Decimal value of given Hexadecimal: 1052

Using an user-defined Method

In this approach, we will define a user-defined method. Then, we will run a for loop till the length of hexadecimal number. In that loop, we will fetch the characters and then we will apply the logic of conversion.

Example

Following example is the practical illustration of the approach discussed above.

public class Conversion {
   public static void cnvrt(String hexNum) {
      // storing all the hexadecimal digits to this string 
      String hexStr = "0123456789ABCDEF"; 
      // converting given argument to uppercase
      hexNum = hexNum.toUpperCase();   
      int dec = 0;  
      for (int i = 0; i < hexNum.length(); i++) {  
         char ch = hexNum.charAt(i); 
         // fetching characters sequentially 
         int index = hexStr.indexOf(ch); 
         // fetching index of characters  
         dec = 16 * dec + index; 
         // applying the logic of conversion 
      }
      System.out.println("Decimal value of given Hexadecimal: " + dec); 
   }
   public static void main(String args[]) {
      // calling the function with arguments
      cnvrt("54A"); 
      cnvrt("41C");  
   }
}

Output of the above code is as follows ?

Decimal value of given Hexadecimal: 1354
Decimal value of given Hexadecimal: 1052
Updated on: 2024-07-31T12:28:11+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements