Cyclic Redundancy Code
Cyclic Redundancy Code import java.util.*;
class U
{
public static String remainder(String data, String divisor) {
int x = 1, z = divisor.length(), j = 0, i;
String data2 = "", strOfZeros = "";
int y = divisor.length() - 1;
/* This is to get correct amount of zero's onto the end of the data */
while (y > 0) {
data += "0";
y--;
}
// Main part of method, this is the long division of Binary numbers.
needToExit: for (i = x, j = 1; i < z && z <= data.length(); i++, j++) {
if (z == data.length() && data2.charAt(0) == '1') {
strOfZeros = "";
for (i = 1; i < divisor.length(); i++) {
if (data2.charAt(i) == divisor.charAt(i))
strOfZeros += '0';
else
strOfZeros += '1';
}
data2 = strOfZeros;
break needToExit;
}
if (data.charAt(i) == divisor.charAt(j))
data2 += "0";
else
data2 += "1";
if (i == divisor.length() - 1) {
data2 += data.charAt(z);
x++;
z++;
// i = x;
j = 0;
// when first bit is a 0
while (data2.charAt(0) != '1' && i == divisor.length() - 1) {
for (i = 1; i < divisor.length(); i++) {
if (data2.charAt(i) == '0')
strOfZeros += "0";
else
strOfZeros += "1";
}
strOfZeros += data.charAt(z);
data2 = strOfZeros;
strOfZeros = "";
x++;
z++;
i = x;
}
}
}
return data2;
}
public static String codeword(String x,String y)
{
StringBuffer d=new StringBuffer(x);
for(int i=0;i<y.length();i++)
{
char c,l;
c=d.charAt(d.length()-i-1);
l=y.charAt(y.length()-i-1);
int a=(int)c-48;
int b=(int)c-48;
int q=a^b;
d.setCharAt((d.length()-i-1),(char)(q+48));
}
String h=d.toString();
return h;
}
}
class CRCC
{
public static void main(String args[])
{
Scanner src=new Scanner(System.in);
System.out.println("enter the data word");
String s=src.nextLine();
System.out.println("enter the divisor");
String x=src.nextLine();
String h=U.remainder(s,x);
System.out.println("the remainder is:"+h);
String a=U.codeword(s,h);
System.out.println("the codewordis:"+a);
Random r=new Random();
String w="";
StringBuffer sb=new StringBuffer(w);
for(int i=0;i<7;i++);
{
if(r.nextBoolean()==true)
sb.append('1');
else
sb.append('0');
}
System.out.println("the recieved bits are:"+sb);
String j=sb.toString();
String k=U.remainder(j,x);
if(k=="000")
System.out.println("No ERROR");
else
System.out.println("ERROR");
}
}
0 comments :