Java Program for Hexadecimal to Binary

Java Program to Convert Hexadecimal to Binary

In this article we will discuss about how to convert Binary to Octal. Before jumping into the program directly, let’s first know about hexadecimal and binary.

Hexadecimal:

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:

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.

Method-1:  Using the key-value-pair method

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: 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

Leave a Comment