The Hexadecimal is a very useful Number System based on the premise of clubbing together 4 bits at a time to constitute a single entity of the system which is composed of 16 symbols including 10 digits ranging from 0-9 and the first six alphabets from A-F. The word Hexadecimal is derived from the words Hex which means six and decimal which means ten.
Thus the combined word denotes sixteen which is six and ten added together. The Hexadecimal Sequences are also referred to as the base or radix 16. While dealing with different Number Systems, it becomes essential to be able to convert them from one system to another. In this article, we focus on converting Hexadecimal to Binary, which is a system comprised of 1’s and 0’s and is the mechanism through which the computers store and process instructions as well as data.
Hexadecimal Sequence : 458A Binary Equivalent : 0100010110001010 Explanation : Binary representation of A : 1010 Binary representation of 8 : 1000 Binary representation of 5 : 0101 Binary representation of 4 : 0100 Hexadecimal Sequence : B36 Binary Equivalent : 101100110110
There are two approaches to convert Hexadecimal to Binary and they are mentioned as follows :
- Using the key-value pair for the corresponding conversion from the Hexadecimal character to its Binary equivalent
- Converting the Hexadecimal to its Decimal equivalent which is further converted to its Binary equivalent
Approach 1 :
Using this approach, we formulate key-value and extracting every character of the Hexadecimal string, add its corresponding binary sequence and return the complete binary sequence.
- Create a HashMap to store the key-value pairs.
- Accept the Hexadecimal sequence as a string and extract each character while iterating through the length of the string.
- Check if the extracted character is present in the keys of the HashMap.
- If it is present, concatenate the string storing the binary sequence with the corresponding value of the key.
- If it is not present, return Invalid Hexadecimal String.
This approach first converts the Hexadecimal String into its decimal equivalent which is further converted into its binary equivalent. We make use of two functions, the first one to convert the Hexadecimal to Decimal and the second one to convert Decimal to Binary.
- First, we write the function to convert the Decimal to Binary.
- Next, we, write the function to convert Hexadecimal to Decimal and call the above function inside this function to convert the converted Decimal further into Binary.
- In this function, we iterate through the length of the Hexadecimal String and extract every character one at a time.
- Next, we check if the extracted characters are in the range 0-9 or A-F.
- If the characters are present in the above-mentioned range we concatenate them to the decimal string.
- Next, the decimal string is converted to Binary using the decimalToBinary() function.
- If even one of the characters is not present in the above-mentioned range, Invalid Hexadecimal String is returned as the output.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.