Java Binary to Hexadecimal
Converting between types in programming is an important task. Moving from one kind to another kind conversion is occasionally necessary. We have discussed numerous conversion types in the section on Java conversions. We may go over the Java method for converting This portion converts binary to hexadecimal.
Hexadecimal Number
The way to write hexadecimal numerals is by prefixing them with 0x in Java programmes.
Java Programs for Binary to Hexadecimal Conversion
In Java, there are two techniques to convert from Binary to Hexadecimal:
- Implementing the Integer.toHexString() Method
- Decimate the transformed number by 16 and divide the remaining.
Using Integer.toHexString()
We have defined two functions in the following Java application, one who can read both the number and other that transforms it from binary to hexadecimal. The provided number was first turned into an integer via the get() function. We have used the toHexString() function of the Integer class to convert the binary integer to a hexadecimal value.
Syntax:
public static String toHexString(int i)
The method takes an integer that it can turn into a string from. The appropriate hexadecimal string is returned.
BinaryToHexadecimal.java
import java.util.Scanner;
class BinaryToHexa
{
int number;
void getNumber()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number: ");
// reads a user-supplied number to convert
number = Integer.parseInt(sc.nextLine(), 3);
}
// function for binary to hexadecimal conversion of the given number
void convert()
{
String hexa = Integer.toHexString(number);
// hexadecimal value is printed
System.out.println("Hexadecimal Value is: " + hexa);
}
}
//main class
public class BinaryToHexadecimal2
{
//main method
public static void main(String args[])
{
BinaryToHexa btoh = new BinaryToHexa();
btoh.getNumber();
btoh.convert();
}
}
Output:
Enter the number: 22222
Hexadecimal Value is: 2f
Algorithm
- Transform the binary number provided into a decimal number (first, extract each digit using by getting the remainder by dividing the number by 10).
- Divide this number by ascending powers of 2.
- Divide the given binary number by 10 repeatedly, omitting the final digit each time.
- Use the toHexString() function to obtain the hexadecimal string after obtaining the decimal integer.
As the name implies, the Hexadecimal number system consists of 16 entities. These 16 entities each have 10 digits, with 0 to 9 standing in for the first 10 hexadecimal numerals. We utilise the English alphabets A through F to represent the numbers 10 to 15 for the remaining 6 numbers. The hexadecimal system's lowest number is 0 and its highest is 15, which is represented by the letter F. A binary number can be converted to a hexadecimal number by adding 4 digits together to form a single hexadecimal character.
Example:
Input: 11011111
Output: DF
- According to the example given above, the binary number 10001101 can be divided into chunks of 4 bits, such as 1000 and 1101, which serve as 2 characters for the equivalent hexadecimal number.
- The resulting hexadecimal number would be 8D. Each letter is decided by finding its decimal equivalent and substituting an alphabet if it is a two-digit number, as it is in this case with D, which stands for 13. The base-16 notation is another name for the hexadecimal system.
We are going to employ the following two methods to convert binary to hexadecimal:
- Using the built-in java function toHexString()
- Obtaining the remainder each time and multiplying the transformed decimal number by 16
Approach:
In this method, the binary number is first converted to a decimal number and then saved as an integer. Then, to create the required output string, we just utilise Java's toHexString() function.
Syntax:
public static String toHexString(int num)
Parameter:
Number to be converted to a Hexadecimal string is specified by the parameter num. The type of data is int.
Value Returned:
The int parameter is converted into a base-16 unsigned integer by the function and returned as a string.
Algorithm:
- Transform the binary value into a decimal value.
- First, separate out each digit from the binary number using the method described above, then divide the result by 10 to get the remainder.
- Next, multiply this number by 2 to the highest power.
- To get rid of the last digit in each iteration, keep dividing the initial binary integer by 10.
- To obtain the appropriate output after obtaining the decimal number, simply utilise the toHexString() method.