Binary to HexaDecimal Program in Java

Advertisements

Prev Tutorial Next Tutorial

Convert Binary to HexaDecimal Program in Java

There are two way to convert binary to hexadecimal in java using predefined method and other is create own logic.

Convert Binary to HexaDecimal in Java

import java.util.Scanner;
 
public class BinaryToHexaDecimal
{
Scanner scan;
int num;
	
void getVal() 
{
System.out.println("Binary to HexaDecimal");
scan = new Scanner(System.in);
		
System.out.println("\nEnter the number :");
num = Integer.parseInt(scan.nextLine(), 2);
}
	
void convert() 
{
		
String hexa = Integer.toHexString(num);
System.out.println("HexaDecimal Value is : " + hexa);
}
}
 
class MainClass 
{
	
public static void main(String args[]) 
{
Binary_Hexa obj = new Binary_Hexa();
obj.getVal();
obj.convert();
}
}

Output

Binary to HexaDecimal

Enter the number :
1010
HexaDecimal Value is : a

Convert Binary to HexaDecimal in Java

import java.util.Scanner;

public class BinaryToHexaDecimal
{
 public static void main(String args[])
 {
  int binnum, rem;
  String hexdecnum="";
        
  // digits in hexadecimal number system
        
   char hex[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
   Scanner scan = new Scanner(System.in);
		
   System.out.print("Enter Binary Number : ");
   binnum = scan.nextInt();
		
   // converting the number in hexadecimal format
   while(binnum>0)
   {
    rem = binnum%16;
    hexdecnum = hex[rem] + hexdecnum;
     binnum = binnum/16;
   }
		
   System.out.print("Equivalent Hexadecimal Value of " +binnum+ " is :\n");
   System.out.print(hexdecnum);
 }
}

Output

Enter Binary Number: 1101

Equivalent Hexadecimal Value of 1101 is: 44D

Syntax to compile and run java program

Syntax

for compile -> c:/>javac BinaryToHexaDecimal.java
for run -> c:/>java BinaryToHexaDecimal

Prev Tutorial Next Tutorial

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements