CYCLIC REDUNDANCY CHECK (CRC)
-------------------------------------------------------
import java.util.*;
class crc
{
public static void main(String args[])
{
int data[],div[],ad[],n,m,i,j,rec[],flag=0;
Scanner cin=new Scanner(System.in);
System.out.print("Enter divisor length :");
n=cin.nextInt();
System.out.print("Enter data length :");
m=cin.nextInt();
div=new int[n+1];
data=new int[m+1];
ad=new int[m+n];
rec=new int[m+n];
System.out.print("Enter dataword :");
for(i=1;i<=n;i++)
{
data[i]=cin.nextInt();
ad[i]=data[i];
}
System.out.print("Enter divisor :");
for(i=1;i<=n;i++)
div[i]=cin.nextInt();
for(i=1;i<=n;i++)
{
if(div[1]==ad[i])
{
for(j=0;j<n;j++)
ad[i+j]=ad[i+j]^div[j+1];
}
else
{
for(j=0;j<n;j++)
ad[i+j]=ad[i+j]^0;
}
}
System.out.print("Codeword :");
for(i=1;i<=n;i++)
System.out.print(data[i]);
for(i=n+1;i<=m+n-1;i++)
System.out.print(ad[i]);
System.out.println("Enter Received codeword :");
for(i=1;i<m+n;i++)
rec[i]=cin.nextInt();
for(i=1;i<=n;i++)
{
if(div[1]==rec[i])
{
for(j=0;j<n;j++)
rec[i+j]=rec[i+j]^div[j+1];
}
else
{
for(j=0;j<n;j++)
rec[i+j]=rec[i+j]^0;
}
}
for(i=m+n-1;i>=1;i--)
{
if(rec[i]==1)
{
flag=1;
System.out.println("Received codeword has error");
break;
}
else
continue;
}
if(flag==0)
System.out.println("Received codeword is correct ");
}
}
---------------------------------------------------
OUTPUT
---------------------------------------------------
Enter divisor length :4
Enter data length :4
Enter dataword :1001
Enter divisor :1011
Codeword :1001110
Enter Received codeword :1001110
Received codeword is correct
Enter divisor length :4
Enter data length :4
Enter dataword :1001
Enter divisor :1011
Codeword :1001110
Enter Received codeword :1001111
Received codeword has error
---------------------------------------------------
No comments:
Post a Comment