Java - Konvert Biner ke Desimal, Oktal dan Heksa
Codingnya:
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication48;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
*
* @author Moch. Afif
*/
public class JavaApplication48 {
/**
* @param args the command line arguments
*/
static int bilDes;
public static void Hexa(int n){
char[] daftarHexa={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
if(n>0){
Hexa(n/16);
System.out.print(daftarHexa[n%16]);//Proses Penghitungan bilangan Hexa
}
}
public static void Oktal(int n){
char[] daftarOktal={'0','1','2','3','4','5','6','7'};
if(n>0){
Oktal(n/8);
System.out.print(daftarOktal[n%8]);//Proses Penghitungan bilangan Oktal
}
}
public static void inputDes(int n){
bilDes=n;
}
public static void tampilHexa(){
System.out.print("\nHexanya :");
Hexa(bilDes);
}
public static void tampilOktal(){
System.out.print("\nOktalnya :");
Oktal(bilDes);
}
public static void main(String arg[]) throws IOException{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Masukan Bilangan Biner :");
String str = bf.readLine();
long num = Long.parseLong(str);
long rem;
while (num>0){
rem=num%10;
num=num/10;
if (rem !=0 && rem !=1){
System.out.print("Buka Bilangan Biner....\n Isi yang benar");
System.exit(0);
}
}
int i=Integer.parseInt(str,2);
System.out.print("Desimalnya :"+i);
inputDes(i);
tampilHexa();
inputDes(i);
tampilOktal();
}
}
Comments
Post a Comment