Java Conversion of Binary Value to Decimal, Octal & HexaDecimal

  1. Binary to Decimal conversion in Java
  2. Binary to Octal conversion in Java
  3. Binary to HexaDecimal 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. Binary to Decimal conversion in Java


Binary_Decimal.java

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

Sample Output

Binary to Decimal

Enter the number :
1010
Decimal Value is : 10




B. Binary to Octal conversion in Java


Binary_Octal.java

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

Sample Output

Binary to Octal

Enter the number :
1010
Octal Value is : 12




C. Binary to HexaDecimal conversion in Java


Binary_Hexa.java

import java.util.Scanner;
 
public class Binary_Hexa {
	
	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();
	}
}

Sample Output

Binary to HexaDecimal

Enter the number :
1010
HexaDecimal Value is : a






SHARE THIS PAGE



product 2

product 3

Feedbacks : balaji.scz@gmail.com