Java Program to Convert Binary to Hexadecimal
Last Updated :
25 Apr, 2023
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
class GFG {
int binaryToDecimal( long binary)
{
int decimalNumber = 0 , i = 0 ;
while (binary > 0 ) {
decimalNumber
+= Math.pow( 2 , i++) * (binary % 10 );
binary /= 10 ;
}
return decimalNumber;
}
String decimalToHex( long binary)
{
int decimalNumber = binaryToDecimal(binary);
String hexNumber
= Integer.toHexString(decimalNumber);
hexNumber = hexNumber.toUpperCase();
return hexNumber;
}
public static void main(String[] args)
{
GFG ob = new GFG();
long num = 10011110 ;
System.out.println( "Inputted number : " +num);
System.out.println(ob.decimalToHex( 10011110 ));
}
}
|
Output
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
class GFG {
int binaryToDecimal( long binary)
{
int decimalNumber = 0 , i = 0 ;
while (binary > 0 ) {
decimalNumber
+= Math.pow( 2 , i++) * (binary % 10 );
binary /= 10 ;
}
return decimalNumber;
}
String decimalToHex( long binary)
{
int decimalNumber = binaryToDecimal(binary);
char arr[] = { 'A' , 'B' , 'C' , 'D' , 'E' , 'F' };
int remainder, i = 0 ;
String hexNumber = "" ;
while (decimalNumber != 0 ) {
remainder = decimalNumber % 16 ;
if (remainder >= 10 )
hexNumber = arr[remainder - 10 ] + hexNumber;
else
hexNumber = remainder + hexNumber;
decimalNumber /= 16 ;
}
return hexNumber;
}
public static void main(String[] args)
{
GFG ob = new GFG();
long num = 11000011 ;
System.out.println( "Input : " +num);
System.out.println( "Output : " +ob.decimalToHex(num));
}
}
|
Output
Input : 11000011
Output : C3
Share your thoughts in the comments
Please Login to comment...