# Hexadecimal to Decimal in Java

Problem Description

Write a Java program to Convert Hexadecimal to Decimal

Decimal Number System: The decimal number system is one of the most popular number system used in day to day life. It is also known as base 10 number system as it uses digits from 0 to 9
that is 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. In order to represent number system, we need a base. Decimal numbers are represented in the base 10. Base 10 is the most common base in the world.

Hexadecimal Number System: The hexadecimal number system is a base-16 numeral system used in computing and mathematics. It uses 16 symbols, including the numbers 0-9 and letters A-F, to represent values from 0 to 15.

Problem Solution

We make a class with two methods one for input and other for conversion and access this by object of this class. We first take the input as Hexadecimal from user. We define a method as convert() to convert the user’s input to Decimal.

Program/Source Code

Here is the source code of the Java Program to Convert Hexadecimal to Decimal. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

1. `/*`
2. ` * Hexadecimal to Decimal Conversion in Java`
3. ` */`
4. ` `
5. `import java.util.Scanner;`
6. `class Hexa_Decimal`
7. `{`
8. `    Scanner scan;`
9. `    int num;`
10. `    void getVal()`
11. `    {`
12. `        System.out.println("HexaDecimal to Decimal");`
13. `        scan = new Scanner(System.in);`
14. `        System.out.println("Enter the number :");`
15. `        num = Integer.parseInt(scan.nextLine(), 16);`
16. `    }`
17. `    void convert()`
18. `    {`
19. `        String decimal = Integer.toString(num);`
20. `        System.out.println("Decimal Value is : " + decimal);`
21. `    }`
22. `}`
23. `class Hexa_Decimal_Main`
24. `{`
25. `    public static void main(String args[])`
26. `    {`
27. `	Hexa_Decimal obj = new Hexa_Decimal();`
28. `	obj.getVal();`
29. `	obj.convert();`
30. `    }`
31. `}`
Program Explanation

1. The program starts by importing the Scanner class from the java.util package.
2. It defines a class called Hexa_Decimal, which will contain the main logic of the program.
3. The Hexa_Decimal class has three member variables: a Scanner object called “scan”, an integer called “num”, and two methods called “getVal” and “convert”.
4. The “getVal” method prompts the user to enter a hexadecimal number and reads it from the standard input using the Scanner object. It then converts the input from a hexadecimal string to an integer using the “parseInt” method of the Integer class.
5. The “convert” method converts the integer value of the hexadecimal number to a decimal string using the “toString” method of the Integer class and prints it to the console.
6. The Hexa_Decimal_Main class contains the main method of the program, which creates an instance of the Hexa_Decimal class, calls its “getVal” method to get the input from the user, and then calls its “convert” method to convert and print the decimal value.

Program Output
```\$ javac Hexa_Decimal_Main.java
\$ java Hexa_Decimal_Main

Enter the number :
12
Decimal Value is : 18```

To practice programs on every topic in Java, please visit “Programming Examples in Java”, “Data Structures in Java” and “Algorithms in Java”.

If you find any mistake above, kindly email to [email protected]