Write a program to convert Hexadecimal to Binary number system


This Java program takes a hexadecimal number as input from the user and converts it into its binary equivalent using the Integer.toBinaryString() method.

The HexatoBinary class contains two methods: getVal() and convert(). The getVal() method takes the input hexadecimal number as a string using the Scanner class and converts it into an integer using the Integer.parseInt() method with a radix of 16.

The convert() method then uses the Integer.toBinaryString() method to convert the hexadecimal number into its binary equivalent. The resulting binary number is stored as a string and then printed to the console using the System.out.println() statement.

The Hexa_Binary class simply creates an instance of the HexatoBinary class and calls the getVal() and convert() methods. Overall, this program is a simple implementation of a hexadecimal to binary converter in Java that utilizes the built-in Integer.toBinaryString() method for conversion.

Source Code

import java.util.Scanner;  
public class Hexa_Binary  
{  
 
	public static void main(String args[])  
	{  
		HexatoBinary obj = new HexatoBinary();
		obj.getVal();  
		obj.convert();  
	}  
}  
class HexatoBinary  
{  
	int hex;  
	void getVal()  
	{  
		Scanner input = new Scanner(System.in);
		System.out.print("Enter Hexdecimal Number : ");
		hex = Integer.parseInt(input.nextLine(), 16); 
	}  
	void convert()  
	{  
		String bin = Integer.toBinaryString(hex);  
		System.out.println("Binary Number : " + bin);  
	}  
}  
 
 

Output

Enter Hexdecimal Number : 1AC
Binary Number : 110101100

Example Programs