Java binary to hex: In the previous article, we have discussed Java Program for Binary to Octal
In this article we will see how to convert Binary to Hexadecimal.
Java Program to Convert Binary to Hexadecimal
So, before going into the program directly, let’s know about binary and hexadecimal.
Binary:
Binary to hexadecimal java: Binary number mainly consists of only two numbers i.e. 0 and 1. The base address of the binary number is 2. For low voltage signal the value will 0 and for the high voltage signal the value will 1.
Example: (1001)2, (111000)2
Hexadecimal:
Hexadecimal number basically defines the base of 16 in the number system. This number is basically consists of 16(sixteen) single digits and alphabets like 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E and F. This number is represented by 4(four) bit only.
Example: (214)16, (178)16
Let’s see different ways to do it.
Method-1: Java Program for Binary to Hexadecimal By using toHexString() method
In this approach the default method toHexString() is used.
import java.util.*;
public class BinaryToHex
{
//binary to decimal convesion
int binaryToDecimal(long binary_no)
{
// for storing the variable conversion
int decimal_Number = 0, i = 0;
// loop to extract the digits of the binary
while (binary_no > 0)
{
//for extraction of the digits
decimal_Number
+= Math.pow(2, i++) * (binary_no % 10);
// updating the binary by eliminating
// the last digit on division by 10
binary_no /= 10;
}
// returning the decimal number
return decimal_Number;
}
// method to convert decimal to hexadecimal
String decimalToHex(long binary_no)
{
// variable to store the output of the
// binaryToDecimal() method
int decimal_Number = binaryToDecimal(binary_no);
// converting the integer to the desired
// hex string using toHexString() method
String hexNumber= Integer.toHexString(decimal_Number);
hexNumber = hexNumber.toUpperCase();
return hexNumber;
}
public static void main(String[] args)
{
// instantiating the class
BinaryToHex ob = new BinaryToHex();
Scanner sc = new Scanner(System.in);
System.out.println("Enter the binary number : ");
long n = sc.nextLong();
System.out.println("After converting the "+n+" is : "+ob.decimalToHex(n));
}
}
Output: Enter the binary number : after converting the 1011111 is: 5F
Method-2: Java Program for Binary to Hexadecimal By repeatedly getting the remainder
In this approach first we will convert the binary number to decimal number. After that continuously dividing and getting the remainder for getting single character for four bit type for the original binary number.
import java.util.*;
public class BinaryToHex
{
// conversion of binary to decimal
int binaryToDecimal(long binary_no)
{
// variable declaration
int decimal_Number = 0, k = 0;
while (binary_no > 0)
{
// extraction of each digit of the binary
decimal_Number+= Math.pow(2, k++) * (binary_no % 10);
binary_no /= 10;
}
// for returning the decimal value
return decimal_Number;
}
// method to convert decimal to hex
String decimalToHex(long binary_no)
{
// for storing the variable output
// binaryToDecimal() method
int decimal_Number = binaryToDecimal(binary_no);
// 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 hex_Number = "";
// loop to convert decimal to hex
while (decimal_Number != 0)
{
// calculating the remainder of decimal
// by dividing by 16
remainder = decimal_Number % 16;
// checking if the remainder is >= 10
if (remainder >= 10)
// replacing with the corresponding
// alphabet from the array
hex_Number = arr[remainder - 10] + hex_Number;
else
hex_Number = remainder + hex_Number;
decimal_Number /= 16;
}
// returning the hex string
return hex_Number;
}
// Driver Code
public static void main(String[] args)
{
// for instantiating the class
BinaryToHex ob = new BinaryToHex();
Scanner sc = new Scanner(System.in);
System.out.println("Enter the binary number : ");
long n = sc.nextLong();
System.out.println("after converting the "+n+" is : "+ob.decimalToHex(n));
}
}
Output: Enter the binary number : after converting the 101111111 is: 17F
Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.
Related Java Programs: