Binary to HexaDecimal Program in Java


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 Advertisements
Download Projects

Google Adsense Advertisements

Buy 3 Year Old Domain, Tutorialday.com, SeosmoTools.com @$200

Buy This Ad Space @$50 per Month, Ad Size 300X250 Contact on: hitesh.xc@gmail.com WhatsApp no. 8882538826

Yahoo Advertisements