Simulation of Error Detection & Correction Code (CRC, Hamming Code)
Simulation of Error Detection & Correction Code (CRC, Hamming Code)
Hamming Code:
Step 1: Start the program.
Step 2: Import net and packages.
Step 3: Get the 7-bit data code
Step 4: Calculation of the number of redundant bits.
Step 5: Positioning the redundant bits.
Step 6: Calculating the values of each redundant bit.
Step 7: Input receiver side data
Step 8: Display error and correct data
CODE:
CRC:
import java.io.*;
class crc
{
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Generator:");
String gen = br.readLine();
System.out.println("Enter Data:");
String data = br.readLine();
String code = data;
while(code.length() < (data.length() + gen.length() - 1))
code = code + "0";
code = data + div(code,gen);
System.out.println("The transmitted Code Word is: " + code);
System.out.println("Please enter the received Code Word: ");
String rec = br.readLine();
if(Integer.parseInt(div(rec,gen)) == 0)
System.out.println("The received code word contains no errors.");
else
System.out.println("The received code word contains errors.");
}
Hamming Code:
import java.util.*;
class HammingCode
{
public static void main(String arg[])
{
Scanner sc=new Scanner(System.in);
System.out.println(“Enter the 7-bit data code”);
int d[]=new int[7];
for(int i=0;i<7;i++)
{
d[i]=sc.nextInt();
}
int p[]=new int[4];
p[0]=d[0]^d[1]^d[3]^d[4]^d[6];
p[1]=d[0]^d[2]^d[3]^d[5]^d[6];
p[2]=d[1]^d[2]^d[3];
p[3]=d[4]^d[5]^d[6];
c[0]=p[0];
c[1]=p[1];
c[2]=d[0];
c[3]=p[2];
c[4]=d[1];
c[5]=d[2];
c[6]=d[3];
c[7]=p[3];
c[8]=d[4];
c[9]=d[5];
c[10]=d[6];
pr[0]=r[0];
pr[1]=r[1];
rd[0]=r[2];
pr[2]=r[3];
rd[1]=r[4];
rd[2]=r[5];
rd[3]=r[6];
pr[3]=r[7];
rd[4]=r[8];
rd[5]=r[9];
rd[6]=r[10];
int s[]=new int[4];
s[0]=pr[0]^rd[0]^rd[1]^rd[3]^rd[4]^rd[6];
s[1]=pr[1]^rd[0]^rd[2]^rd[3]^rd[5]^rd[6];
s[2]=pr[2]^rd[1]^rd[2]^rd[3];
s[3]=pr[3]^rd[4]^rd[5]^rd[6];
int dec=(s[0]*1)+(s[1]*2)+(s[2]*4)+(s[3]*8);
if(dec==0)
System.out.println(“No error”);
else
{
System.out.println(“Error is at “+dec);
if(r[dec-1]==0)
r[dec-1]=1;
else
r[dec-1]=0;
}
System.out.println(“Corrected code word is : “);
for(int i=0;i<11;i++)
System.out.print(r[i]+” “);
System.out.println();
}
}
OUTPUT:
CRC:
Hamming Code:
DIKSHA NASA
RA1911042010003
CSBS-R1