The Hexadecimal number system as the name suggests comprises 16 entities. These 16 entities consist of 10 digits, 0-9 representing the first 10 numbers of the hexadecimal system as well. For the remaining 6 numbers, we use English alphabets ranging from A through F to represent the numbers 10 to 15. It should be noted that the lowest number in the hexadecimal system is 0 with the highest being 15 represented by F. A hexadecimal number can be derived from a binary number by clubbing 4 digits to constitute a single character of the hexadecimal number.
Input: 11011111 Output: DF Input: 10001101 Output: 8D
- In the above example, the binary number 10001101 can be broken down into chunks of 4 bits such as 1000 and 1101 which act as 2 characters for the corresponding hexadecimal number.
- The resultant hexadecimal number would be 8D where every character is determined by calculating its corresponding value in the decimal system and replacing it with an alphabet if it is a two-digit number in this case D which represents 13. The hexadecimal system is also referred to as base-16.
For the conversion of binary to hexadecimal, we are going to use the following two approaches :
- Using the toHexString() builtin java method
- Repeatedly getting the remainder and dividing the converted decimal number by 16
Using this approach, we first convert the binary number to a decimal number which is stored as an Integer. Then, we simply use the toHexString() method of java to generate the desired output string.
public static String toHexString(int num)
- num – This parameter specifies the number which is to be converted
to a Hexadecimal string. The data-type is int.
Return Value: The function returns a string representation of the int argument as an unsigned integer in base 16.
- Convert the binary number to a decimal number.
- To convert the binary number to a decimal number, first, extract each digit using by getting the remainder by dividing by 10.
- Next, multiply this digit with increasing powers of 2.
- Keep on dividing the original binary number by 10 to eliminate the last digit in each iteration.
- After having gotten the decimal number, just use the toHexString() method to get the desired output.
Inputted number : 10011110 9E
- Under the second approach, we again first convert the binary number to a decimal number.
- Then we continuously divide and get the remainder of this decimal number to get the single character for each set of 4 bits we can find in the original binary number.
- Convert the binary to a decimal using steps 2-4 in the above algorithm.
- Next, run a while loop with the terminating condition that the decimal number becomes 0 and the updating condition that the decimal number is divided by 16 in each iteration.
- In each iteration get the remainder by dividing the number by 16.
- Constitute a new String and keep on adding characters which are the remaining on dividing by 16.
- If the remainder is greater than or equal to 10 replace it with alphabets A-F depending on the remainder.
Input : 11000011 Output : C3
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.