Java Conversion of HexaDecimal Value to Binary, Decimal & Octal

  1. HexaDecimal to Binary conversion in Java
  2. HexaDecimal to Decimal conversion in Java
  3. HexaDecimal to Octal conversion in Java

Note
Here I am using,
OS : Linux (Ubuntu 12.04)
IDE : Eclipse Tool
Eclipse : Juno (Version 4.2.0)
Package : Default package

A. HexaDecimal to Binary conversion in Java


Hexa_Binary.java

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

Sample Output

HexaDecimal to Binary

Enter the number :
20
Binary Value is : 100000




B. HexaDecimal to Decimal conversion in Java


Hexa_Decimal.java

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

Sample Output

HexaDecimal to Decimal

Enter the number :
20
Decimal Value is : 32




C. HexaDecimal to Octal conversion in Java


Hexa_Octal.java

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

Sample Output

HexaDecimal to Octal

Enter the number :
20
Octal Value is : 40






SHARE THIS PAGE



product 2

product 3

Feedbacks : balaji.scz@gmail.com