Java Conversion of Decimal Value to Binary, Octal & HexaDecimal

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


Decimal_Binary.java

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

Sample Output

Decimal to Binary

Enter the number :
530
Binary Value is : 1000010010




B. Decimal to Octal conversion in Java


Decimal_Octal.java

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

Sample Output

Decimal to Octal

Enter the number :
148
Octal Value is : 224




C. Decimal to HexaDecimal conversion in Java


Decimal_Hexa.java

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

Sample Output

Decimal to HexaDecimal

Enter the number :
15
HexaDecimal Value is : f






SHARE THIS PAGE



product 2

product 3

Feedbacks : balaji.scz@gmail.com