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.
Example:
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
Approach 1:
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.
Syntax :
public static String toHexString(int num)
Parameter:
- 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.
Algorithm :
- 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.
Example:
Java
// Java program to convert binary to hexadecimal class GFG { // method to convert binary to decimal int binaryToDecimal( long binary) { // variable to store the converted // binary number int decimalNumber = 0 , i = 0 ; // loop to extract the digits of the binary while (binary > 0 ) { // extracting the digits by getting // remainder on dividing by 10 and // multiplying by increasing integral // powers of 2 decimalNumber += Math.pow( 2 , i++) * (binary % 10 ); // updating the binary by eliminating // the last digit on division by 10 binary /= 10 ; } // returning the decimal number return decimalNumber; } // method to convert decimal to hexadecimal String decimalToHex( long binary) { // variable to store the output of the // binaryToDecimal() method int decimalNumber = binaryToDecimal(binary); // converting the integer to the desired // hex string using toHexString() method String hexNumber = Integer.toHexString(decimalNumber); // converting the string to uppercase // for uniformity hexNumber = hexNumber.toUpperCase(); // returning the final hex string return hexNumber; } // Driver Code public static void main(String[] args) { // instantiating the class GFG ob = new GFG(); long num = 10011110 ; // calling and printing the output // of decimalToHex() method System.out.println( "Inputted number : " +num); System.out.println(ob.decimalToHex( 10011110 )); } } |
Inputted number : 10011110 9E
Time complexity: O(log N) where N is the input binary number, since the number of iterations in the while loop is proportional to the number of digits in the binary number.
Auxiliary space: O(1)
Approach 2:
- 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.
Algorithm:
- 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.
Example:
Java
// Java program to convert binary to hexadecimal class GFG { // method to convert binary to decimal int binaryToDecimal( long binary) { // variable to store the converted binary int decimalNumber = 0 , i = 0 ; // loop to extract digits of the binary while (binary > 0 ) { // extracting each digit of the binary // by getting the remainder of division // by 10 and multiplying it by // increasing integral powers of 2 decimalNumber += Math.pow( 2 , i++) * (binary % 10 ); // update condition of dividing the // binary by 10 binary /= 10 ; } // returning the decimal return decimalNumber; } // method to convert decimal to hex String decimalToHex( long binary) { // variable to store the output of // binaryToDecimal() method int decimalNumber = binaryToDecimal(binary); // character array to represent double // digit remainders char arr[] = { 'A' , 'B' , 'C' , 'D' , 'E' , 'F' }; // variable to store the remainder on // division by 16 int remainder, i = 0 ; // declaring the string that stores the // final hex string String hexNumber = "" ; // loop to convert decimal to hex while (decimalNumber != 0 ) { // calculating the remainder of decimal // by dividing by 16 remainder = decimalNumber % 16 ; // checking if the remainder is >= 10 if (remainder >= 10 ) // replacing with the corresponding // alphabet from the array hexNumber = arr[remainder - 10 ] + hexNumber; else hexNumber = remainder + hexNumber; // update condition of dividing the // number by 16 decimalNumber /= 16 ; } // returning the hex string return hexNumber; } // Driver Code public static void main(String[] args) { // instantiating the class GFG ob = new GFG(); long num = 11000011 ; // printing and calling the // decimalToHex() method System.out.println( "Input : " +num); System.out.println( "Output : " +ob.decimalToHex(num)); } } |
Input : 11000011 Output : C3