Hexadecimal to binary java – Java Program for Hexadecimal to Binary

Java hex to binary: In the previous article, we have discussed Java Program for Octal to Hexadecimal

In this article we will discuss about how to convert Binary to Octal.

Java Program to Convert Hexadecimal to Binary

Before jumping into the program directly, let’s first know about hexadecimal and binary.

Hexadecimal:

Hexadecimal to binary java: Hexadecimal number basically defines the base of 16 in the number system. This number is basically consists of 16(sixteen) single digits and alphabets like 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E and F. This number is represented by 4(four) bit only.

Example:

(214)16
(178)16

Binary:

Convert hex to binary java: Binary number mainly consists of only two numbers i.e. 0 and 1. The base address of the binary number is 2. For low voltage signal the value will 0 and for the high voltage signal the value will 1.

Example:

(1001)2
(111000)2

Let’s see different ways to convert hexadecimal to binary.

Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.

Method-1: Java Program for Hexadecimal to Binary Using the key-value-pair method

hex to binary java: In this type hashMap is used where key value pair helps to achieve binary equivalent After that hexadecimal converts to decimal and then to binary equivalent.

import java.util.*;

import java.util.HashMap;

public class HexadecimalToBinary
{
    // conversion of Hexadecimal to Binary
    String hexToBinary(String hex)
    {
        // variable to store the converted
        // Binary Sequence
        String binary_no = "";
        // converting the accepted Hexadecimal
        // string to upper case
        hex = hex.toUpperCase();
        // initializing the HashMap class
        HashMap<Character, String> hashMap= new HashMap<Character, String>();
        // storing the key value pairs
        hashMap.put('0', "0000");
        hashMap.put('1', "0001");
        hashMap.put('2', "0010");
        hashMap.put('3', "0011");
        hashMap.put('4', "0100");
        hashMap.put('5', "0101");
        hashMap.put('6', "0110");
        hashMap.put('7', "0111");
        hashMap.put('8', "1000");
        hashMap.put('9', "1001");
        hashMap.put('A', "1010");
        hashMap.put('B', "1011");
        hashMap.put('C', "1100");
        hashMap.put('D', "1101");
        hashMap.put('E', "1110");
        hashMap.put('F', "1111");
        int k;
        char character1;
        // loop to iterate through the length of the Hexadecimal String
        for (k = 0; k < hex.length(); k++) 
        {
            // extracting each character
            character1 = hex.charAt(k);
            // checking if the character is present in the keys
            if (hashMap.containsKey(character1))
            binary_no += hashMap.get(character1);
            //for returning Invalid Hexadecimal
            else
            {
                binary_no = "Invalid Hexadecimal String";
                return binary_no;
            }
        }
        // for returning the converted Binary
        return binary_no;
    }
    public static void main(String[] args)
    {
        // instantiating the class
        HexadecimalToBinary ob = new HexadecimalToBinary();
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the Hexadecimal number : ");
        String n = sc.nextLine();
        System.out.println("After converting the "+n+" is:");
        System.out.println(n.toUpperCase());
        System.out.println(ob.hexToBinary(n));
    }
}
Output:

Enter the Hexadecimal number : abcdef
after converting the ABCDEF is : 101010111100110111101111

Method-2: Java Program for Hexadecimal to Binary Using Integer.toBinaryString() method

import java.util.Scanner;

public class HexadecimalToBinary
{
    Scanner sc=new Scanner(System.in);
    int num;
    //hexadecimal inputted
    void inputvalue()
    {
    System.out.println("HexaDecimal to Binary");
    System.out.println("\nEnter the number :");
    num = Integer.parseInt(sc.nextLine(), 16);
    }
    //converted to binary
    void convert()
    {
    String binary = Integer.toBinaryString(num);
    System.out.println("Binary Value is : " + binary);
    }

    //driver method
    public static void main(String args[])
    {
    	HexadecimalToBinary obj = new HexadecimalToBinary();
    	obj.inputvalue();
    	obj.convert();
    }
}
Output:

Enter the number : 216
Binary Value is : 1000010110

Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic.

Related Java Programs: