import java.io.*;
import java.util.*;
public class konversi
{
public static String ConversiDes_Bin(int des)
{
if(des==1)
return Character.toString('1');
else if ((des%2)==1)
return ConversiDes_Bin(des/2)+ConversiDes_Bin(1);
else
return ConversiDes_Bin(des/2)+Character.toString('0');
}
public static int ConversiBin_Des(String bin)
{
int i, a=0;
for (i=0; i < bin.length(); i++)
{
if (bin.charAt(i)=='1')
a+=Math.pow(2,(bin.length()-1)-i);
else
a+=0;
}
return a;
}
public static String ConversiDes_Hexa(int des)
{
String b,c;
b=String.valueOf(des/16);
c=String.valueOf(des%16);
if (des==0)
return Character.toString('0');
else if(des==1)
return Character.toString('1');
else if(des==2)
return Character.toString('2');
else if(des==3)
return Character.toString('3');
else if(des==4)
return Character.toString('4');
else if(des==5)
return Character.toString('5');
else if(des==6)
return Character.toString('6');
else if(des==7)
return Character.toString('7');
else if(des==8)
return Character.toString('8');
else if(des==9)
return Character.toString('9');
else if(des==10)
return Character.toString('A');
else if(des==11)
return Character.toString('B');
else if(des==12)
return Character.toString('C');
else if(des==13)
return Character.toString('D');
else if(des==14)
return Character.toString('E');
else if(des==15)
return Character.toString('F');
else if (((des%16) < 10)&&((des/16) < 10))
return b+c;
else if (((des%16) < 10)&&((des/16) >= 10))
return (ConversiDes_Hexa(des/16)+ConversiDes_Hexa(des%16));
else if (((des%16) >=10)&&((des/16) < 10))
return b+ConversiDes_Hexa(des%16);
else if (((des%16) >=10)&&((des/16) >=10))
return ConversiDes_Hexa(des%16)+ConversiDes_Hexa(des%16);
else
return "";
}
public static int ConversiHexa_Des(String hex)
{
int i, j, maks=hex.length();
char a;
if (hex.equals(Character.toString('0')))
return 0;
else if(hex.equals(Character.toString('1')))
return 1;
else if(hex.equals(Character.toString('2')))
return 2;
else if(hex.equals(Character.toString('3')))
return 3;
else if(hex.equals(Character.toString('4')))
return 4;
else if(hex.equals(Character.toString('5')))
return 5;
else if(hex.equals(Character.toString('6')))
return 6;
else if(hex.equals(Character.toString('7')))
return 7;
else if(hex.equals(Character.toString('8')))
return 8;
else if(hex.equals(Character.toString('9')))
return 9;
else if(hex.equals(Character.toString('A')))
return 10;
else if(hex.equals(Character.toString('B')))
return 11;
else if(hex.equals(Character.toString('C')))
return 12;
else if(hex.equals(Character.toString('D')))
return 13;
else if(hex.equals(Character.toString('E')))
return 14;
else if(hex.equals(Character.toString('F')))
return 15;
else
return
((ConversiHexa_Des(Character.toString(hex.charAt(0)))*16)
+ConversiHexa_Des(Character.toString(hex.charAt(1))));
}
public static String ConversiHexa_Bin(String heks)
{
int a;
String k;
a=(ConversiHexa_Des(heks));
k=ConversiDes_Bin(a);
return k;
}
public static String ConversiBin_Hexa(String biner)
{
int i;
String j;
i=ConversiBin_Des(biner);
j=ConversiDes_Hexa(i);
return j;
}
public static void main(String args[])
{
String m;
int pilih, i;
do
{
// Menu program
System.out.println("Menu Konversi Bilangan ");
System.out.println("1. Desimal ke Heksadesimal");
System.out.println("2. Heksadesimal ke Desimal");
System.out.println("3. Desimal ke Biner");
System.out.println("4. Biner ke Desimal");
System.out.println("5. Heksadesimal ke Biner");
System.out.println("6. Biner ke Heksadesimal");
System.out.println("0. Keluar");
System.out.print("Pilih menu ");
Scanner plh = new Scanner(System.in);
pilih=plh.nextInt();
switch (pilih)
{
case 1 :
System.out.println("Desimal |Heksadesimal");
for (i=0; i < 256; i++)
{
System.out.println(+i+" |"+ConversiDes_Hexa(i));
}
break;
case 2 :
System.out.print("Masukkan bilangan heksadesimal "
+"= ");
Scanner heks = new Scanner (System.in);
m=heks.nextLine();
System.out.println(""+ConversiHexa_Des(m));
break;
case 3 :
System.out.println("Desimal |Biner");
for (i=1; i < 256; i++)
{
System.out.println(+i+" |"+ConversiDes_Bin(i));
}
break;
case 4 :
System.out.print("Masukkan bilangan biner = ");
Scanner bin = new Scanner (System.in);
m=bin.nextLine();
System.out.println(""+ConversiBin_Des(m));
break;
case 5 :
System.out.print("Masukkan bilangan "
+"heksadesimal = ");
Scanner hex = new Scanner (System.in);
m=hex.nextLine();
System.out.println(""+ConversiHexa_Bin(m));
break;
case 6 :
System.out.print("Masukkan bilangan biner = ");
Scanner binsa = new Scanner (System.in);
m=binsa.nextLine();
System.out.println(""+ConversiBin_Hexa(m));
break;
}
}
while(pilih!=0);
}
}
Source : Pak Rudi Hartono
0 komentar:
Posting Komentar