Java Program to Convert Binary to Hexadecimal
Last Updated :
02 Aug, 2024
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));
}
}
OutputInputted 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));
}
}
OutputInput : 11000011
Output : C3
Similar Reads
Program to Convert Octal to Hexadecimal Given an Octal number, the task is to convert it into a Hexadecimal number. Examples: Input: 47 Output: 27Explanation:Decimal value of 47 is = (7 * 1) + (4 * 8) = 39Now, convert this number to hexadecimal39/16 -> quotient = 2, remainder = 72/16 -> quotient = 0, remainder = 2So, the equivalent
15+ min read
Program to convert IP address to hexadecimal Given an IP Address and task is to change the IP address equivalent to the hexadecimal value. Examples: Input : 127.0.0.1Output : 0x7f000001Input : 172.31.0.2Output : 0xac1f0002Explanation: Using the Library function to convert the IP address into the hexadecimal value we use the " arpa/inet.h " hea
4 min read
Java Program to Generate Random Hexadecimal Bytes To generate Random Hexadecimal Bytes, first, a random byte can be generated in decimal form using Java.util.Random.nextInt() and then it can be converted to hexadecimal form using Integer.toHexString() method. 1. Java.util.Random.nextInt() The nextInt() method is used to obtain the next integer from
2 min read
Java Program to Convert Octal to Binary Given an Octal number as input, the task is to convert that number into its Binary equivalent number. Example: Input: Octal Number = 513 Output: Binary equivalent value is: 101001011 Explanation : Binary equivalent value of 5: 101 Binary equivalent value of 1: 001 Binary equivalent value of 3: 011Oc
5 min read
Java Program to Convert Binary to Octal A Binary (base 2) number is given, and our task is to convert it into an Octal (base 8) number. There are different ways to convert binary to decimal, which we will discuss in this article.Example of Binary to Octal Conversion:Input : 100100Output: 44Input : 1100001Output : 141A Binary Number System
5 min read
Java Program to Convert Char to Int Given a char value, and our task is to convert it into an int value in Java. We can convert a Character to its equivalent Integer in different ways, which are covered in this article.Examples of Conversion from Char to Int:Input : ch = '3'Output : 3Input : ch = '9'Output : 9The char data type is a s
4 min read
Java Program to Convert Integer Values into Binary Given an integer in Java, your task is to write a Java program to convert this given integer into a binary number. Example: Input: = 45 Output: = 101101 Input: = 32 Output: = 100000 Integers: Integers are numbers whose base value is 10. The Integer or int data type is a 32-bit signed twoâs complemen
4 min read
Java Program to Convert Char to Byte Given a char in Java, the task is to write a Java program that converts this char into Byte. Examples: Input: ch = 'A' Output: 65 Input: ch = 'B' Output 66 In Java, char is a primitive data type and it is used to declare characters. It has the capability to hold 16-bit unsigned Unicode characters. T
3 min read
Java Program to Convert Byte Array to Hex String Byte Array - A Java Byte Array is an array used to store byte data types only. The default value of each element of the byte array is 0. Hex String - A Hex String is a combination of the digits 0-9 and characters A-F, just like how a binary string comprises only 0's and 1's. Eg: "245FC" is a hexadec
5 min read
Java Program to Convert Hex String to Byte Array Hex String - A Hex String is a combination of the digits 0-9 and characters A-F, just like how a binary string comprises only 0's and 1's. Eg: "245FC" is a hexadecimal string. Byte Array - A Java Byte Array is an array used to store byte data types only. The default value of each element of the byte
4 min read