×

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

  1. Transform the binary number provided into a decimal number (first, extract each digit using by getting the remainder by dividing the number by 10).
  2. Divide this number by ascending powers of 2.
  3. Divide the given binary number by 10 repeatedly, omitting the final digit each time.
  4. 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:

  1. Using the built-in java function toHexString()
  2. 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.

Related Topics

Swapping Program in Java

Swapping Program in Java The swapping program in Java is used to interchange the values of the two variables. For example, if X = 12 and Y = 24, then the...

4 minutes read.

Gregorian Calendar Java Current Date

GregorianCalendar class uses the Gregorian and Julian calendars. Dates are calculated by projecting present laws forever backward and forward in time. As a consequence, GregorianCalendar may be utilised to create...

8 minutes read.

Java 8 Features

Java 8 Features: After the great success of Java 7, many developers were waiting for the next version of one of the best languages in the tech world. Moreover, on...

6 minutes read.

How to create array of objects in Java

Java is an object-oriented programming language therefore everything in Java is based on objects and classes. Array is a data structure that holds data of similar type and dynamically creates...

4 minutes read.

Java Integer remainder Unsigned() method

The remainderUnsigned() method of Java Integer class returns the unsigned remainder by dividing the first and second argument. Syntax public static int remainderUnsigned(int dividend, int divisor)  Parameters The ‘dividend’ and ‘divisor’ represents the value...

1 minute read.

Java Program to generate binary numbers

A binary tree can create binary numbers ranging from 1 to n. Every node in a tree, the right, and left nodes, has two children, as is common knowledge. The...

3 minutes read.

How to check version of java in Linux

Java is one of the most famous and thoroughly utilized programming tongues from one side of the world to the other. On the off chance that you are a Java...

2 minutes read.

Java String valueOf() method

Java String valueOf() method converts different types of values into String. Such as : int to String, long to String, boolean to String, character to String, float to String, double to...

2 minutes read.

Java Code Coverage Tools

Code coverage testing is a crucial metric that gauges how thoroughly the program's source code has been tested. The market is flooded with Code Coverage Tools, making it difficult to...

7 minutes read.

Java vs Node.js

Java: Java is an object oriented programming language. It is also known as multi threaded language. It was designed by James gosling in the year 1995. We can also say that...

4 minutes read.

Java Integer signum() method

The signum() method of Java Integer class returns the signum function of the specified int value. Syntax public static int signum (int i)  Parameters The parameter ‘i’ represents the value whose signum is to...

1 minute read.

Java CountDownLatch

Another crucial classes for concurrent execution is CountDownLatch. It is a synchronisation tool that enables one or more threads to await until a series of tasks started by another thread...

4 minutes read.

Hourglass problem in Java

In this section, we will discuss the hourglass problem in Java.The aim is to find the largest sum of an hour glass given a 2D matrix. An hour glass is made...

2 minutes read.

Java LinkedList vs ArrayList

LinkedList In LinkedList, each element is a distinct entity containing an information portion and an address component, and the elements are not kept in consecutive locations. Pointers & addresses are used...

3 minutes read.

Program to Reverse a Number in Java

In order to reverse a number, the digit in the first place must be swapped with the digit in the final position, the second digit with the second-to-last digit, and...

3 minutes read.

Java 9 Interface Private Method

Java 9 provides us the facility to include private methods inside the java interface. In java 8 and earlier versions, we are supposed to use only constant variables and abstract...

3 minutes read.

Java Int Keyword

Among the primitive data types is the Java int keyword. To declare variables, use this. It can also be used with methods that return values of the integer type. It...

3 minutes read.

Java File

Java file class implements the concept of file handling. It has several methods, such as deleting, creating, reading, and updating files. This class allows java users to perform various operations...

5 minutes read.

Java Obfuscator

Obfuscation is the process of making something unclear or difficult to interpret. Obfuscators are being used in programming to protect the source code from hackers. In this article, we will...

8 minutes read.

Java Thread Lifecycle: States and Stages

Multithreading Multithreading is one of the most important features in object-oriented programming, and this multithreading can be implemented by various object-oriented programming languages like Java, Python, and C++. A multithreaded process...

7 minutes read.