Nama : Husnul Khotimah
Nim : 2010.69.04.0004
Mata kuliah : Program simulasi dan permodelan
Class untuk
konversi :
import
java.util.Scanner;
class konversi {
int ascii;
int ASCII = 0;
int bincs = 0;
String data = "";
String BINCS = "";
String DATA = "";
///////////////////////////////////////////////////////
public int A2I(char huruf) {
ascii = (int) huruf;
return ascii;
}
///////////////////////////////////////////////////////
public String I2B() {
String biner;
if (ascii >= 128){
biner = Integer.toBinaryString(ascii);
data = data+biner;
return biner;
}
else if (ascii >= 64) {
biner = Integer.toBinaryString(ascii);
String biner2 = "0" + biner;
data = data+biner2;
return biner2;
}
else if (ascii >= 32) {
biner = Integer.toBinaryString(ascii);
String biner3 = "00" + biner;
data = data+biner3;
return biner3;
}
else if (ascii >= 16) {
biner = Integer.toBinaryString(ascii);
String biner4 = "000" + biner;
data = data+biner4;
return biner4;
}
else if (ascii >= 8) {
biner = Integer.toBinaryString(ascii);
String biner5 = "0000" + biner;
data = data+biner5;
return biner5;
}
else if (ascii >= 4) {
biner = Integer.toBinaryString(ascii);
String biner6 = "00000" + biner;
data = data+biner6;
return biner6;
}
else if (ascii >= 2) {
biner = Integer.toBinaryString(ascii);
String biner7 = "000000" + biner;
data = data+biner7;
return biner7;
}
else {
biner = Integer.toBinaryString(ascii);
String biner8 = "0000000" + biner;
data = data+biner8;
return biner8;
}
}
///////////////////////////////////////////////////////
public int error() {
bincs = (ascii ^ bincs);
return bincs;
}
///////////////////////////////////////////////////////
public String B2B() {
String bnr;
error();
if (bincs >= 128){
bnr = Integer.toBinaryString(bincs);
BINCS = bnr;
return (bnr);
}
else if (bincs >= 64) {
bnr = Integer.toBinaryString(bincs);
String bnr2 = "0" + bnr;
BINCS = bnr2;
return (bnr2);
}
else if (bincs >= 32){
bnr = Integer.toBinaryString(bincs);
String bnr3 = "00" + bnr;
BINCS = bnr3;
return (bnr3);
}
else if (bincs >= 16){
bnr = Integer.toBinaryString(bincs);
String bnr4 = "000" + bnr;
BINCS = bnr4;
return (bnr4);
}
else if (bincs >= 8){
bnr = Integer.toBinaryString(bincs);
String bnr5 = "0000" + bnr;
BINCS = bnr5;
return (bnr5);
}
else if (bincs >= 4){
bnr = Integer.toBinaryString(bincs);
String bnr6 = "00000" + bnr;
BINCS = bnr6;
return (bnr6);
}
else if (bincs >= 2){
bnr = Integer.toBinaryString(bincs);
String bnr7 = "000000" + bnr;
BINCS = bnr7;
return (bnr7);
}
else {
bnr = Integer.toBinaryString(bincs);
String bnr8 = "0000000" + bnr;
BINCS = bnr8;
return (bnr8);
}
}
}
//////////////////////////////////////////////////////////////////////////////////
Main class
untuk program :
public class
Kom_Dat{
public static void main( String[] args ) {
Scanner baca = new Scanner(System.in);
konversi kv = new konversi();
String huruf;
System.out.print("Masukkan Kata = ");
huruf = baca.nextLine();
System.out.println();
char[] kar = huruf.toCharArray();
System.out.print("Data dikirim ---> "+huruf);
System.out.println("");
System.out.print("Binary --->
");
for (int i=0; i
kv.A2I(kar[i]);
System.out.printf(kv.I2B());
}
System.out.println("");
System.out.print("Check Parity ---> ");
for (int i=0; i
kv.A2I(kar[i]);
String kata=kv.I2B();
String hrf;
for(int a=0;a
hrf=
kata.substring(a,a+1);
if(hrf.equals("0")){
System.out.print("1");
}else{
System.out.print("0");
}
}
}
System.out.println("");
System.out.print("Data Diterima ---> " );
for (int i=0; i
kv.A2I(kar[i]);
System.out.printf(kv.I2B());
}
System.out.println("");
System.out.print("Data diterima ---> "+huruf);
}
}
Komentar
Posting Komentar