In this section, you will learn to convert binary data into hexadecimal.
Convert Binary to Hexadecimal
In this section, you will learn to convert binary data into hexadecimal. The java.lang package provides the functionality to convert the binary data into hexadecimal.
Description of the program:
This program takes a binary data from user, which is a string type that is converted into the long data using the parseLong() method. It takes string type value and converts it into a long data. The condition [ if(rem != 0 && rem != 1) ] checks whether the given number is binary number or not. If the given number is either '0' or '1' , program converts the binary data into hexadecimal using toHexaString() method. If fails, it displays a message "This is not a binary number" or "Please try once again.".
toHexString():
This is the method that
takes an integer type value and converts it into binary to hexadecimal. The base
of hexadecimal is 16.
Here is the code of this program:
import java.io.*;
|
Output of this program:
C:\corejava>java BinaryToHexa Enter the Binary number: 1111111 Hexa decimal: 7f C:\corejava>_ |