Two D Parity
Sender Side:
package TwoD;
import java.io.*;
import java.util.*;
public class Sender {
public static void main(String[] args) throws IOException {
Scanner s=new Scanner(System.in);
File f=new File("src/TwoD/msg.txt");
FileWriter fw=new FileWriter(f);
BufferedWriter bw=new BufferedWriter(fw);
System.out.println("Enter Your Message:");
String msg=s.nextLine();
char[]msgg=msg.toCharArray();
String []binary=new String[msg.length()+1];
int rBit=0;
for (int i = 0; i < msg.length(); i++) {
binary[i]=Integer.toBinaryString((int)msgg[i]);
while(binary[i].length()<8){
binary[i]="0"+binary[i];
}
char []ch=binary[i].toCharArray();
for (int j = 0; j < ch.length; j++) {
rBit=rBit^Integer.parseInt(ch[j]+"");
}
binary[i]+=rBit;
rBit=0;
}
int cBit=0;
binary[binary.length-1]="";
for (int i = 0; i < 9; i++) {
for (int j = 0; j < msg.length(); j++) {
cBit=cBit^Integer.parseInt(binary[j].toCharArray()[i]+"");
}
binary[binary.length-1]+=cBit;
cBit=0;
}
for (int i = 0; i < binary.length; i++) {
bw.write(binary[i]);
bw.newLine();
}
bw.flush();
bw.close();
fw.close();
}
}
run:
Enter Your Message:
smit shah
BUILD SUCCESSFUL (total time: 4 seconds)
FILE:
011100111
011011011
011010010
011101000
001000001
011100111
011010001
011000011
011010001
001100011
Receiver Side:
package TwoD;
import java.io.*;
import java.util.*;
public class Receiver {
public static void main(String[] args) throws FileNotFoundException, IOException {
Scanner s=new Scanner(System.in);
File f=new File("src/TwoD/msg.txt");
FileReader fr=new FileReader(f);
BufferedReader br=new BufferedReader(fr);
ArrayList binary=new ArrayList();
String temp;
while((temp=br.readLine())!=null)
{
while(temp.length()<9)
{
temp="0"+temp;
}
binary.add(temp);
}
int rBit=0;
String []bArray=new String[binary.size()];
for (int i = 0; i < bArray.length; i++) {
bArray[i]=binary.get(i).toString();
for (int j = 0; j < 9; j++) {
rBit=rBit^Integer.parseInt(bArray[i].toCharArray()[j]+"");
}
if(rBit==1)
{
System.out.println("Your Data is Currupted");
return;
}
}
int cBit=0;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < bArray.length; j++) {
cBit=cBit^Integer.parseInt(bArray[j].toCharArray()[i]+"");
}
if(cBit==1)
{
System.out.println("Your Data is Currupted");
return;
}
}
StringBuilder msg=new StringBuilder();
for (int i = 0; i < bArray.length-1; i++) {
int ascii=Integer.parseInt(bArray[i].substring(0,8),2);
msg.append((char)ascii);
}
System.out.println("Your message is:"+msg);
}
}
run:
Your message is:smit shah
BUILD SUCCESSFUL (total time: 0 seconds)
Comments
Post a Comment