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.
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.
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.
/*
* Hexadecimal to Decimal Conversion in Java
*/
import java.util.Scanner;
class Hexa_Decimal
{
Scanner scan;
int num;
void getVal()
{
System.out.println("HexaDecimal to Decimal");
scan = new Scanner(System.in);
System.out.println("Enter the number :");
num = Integer.parseInt(scan.nextLine(), 16);
}
void convert()
{
String decimal = Integer.toString(num);
System.out.println("Decimal Value is : " + decimal);
}
}
class Hexa_Decimal_Main
{
public static void main(String args[])
{
Hexa_Decimal obj = new Hexa_Decimal();
obj.getVal();
obj.convert();
}
}
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.
$ javac Hexa_Decimal_Main.java $ java Hexa_Decimal_Main HexaDecimal to Decimal 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”.
- Check Programming Books
- Practice BCA MCQs
- Check Java Books
- Practice Information Technology MCQs
- Practice Programming MCQs