Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
16 views3 pages

Lab Program 7

Uploaded by

shilpakv2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views3 pages

Lab Program 7

Uploaded by

shilpakv2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

7 .

Write a program for error detecting code using CRC-CCITT (16 BITS)

import java.util.Arrays;
import java.util.Scanner;

public class crc {

static int crc(String p,int mode)


{

int j,k;
String poly="10000100000010001";

String zero="0000000000000000";

char output[]=new char[p.length()];


char input[]=new char[p.length()];
char g[]=new char[poly.length()];

String out=null;
out=p;

g=poly.toCharArray();

if(mode==1)
out=out+zero;

output=out.toCharArray();
String res=null;

for(j=0;j<input.length;j++)
if(output[j]=='1')
for(k=0;k<g.length;k++)

if((output[j+k]=='1'&&g[k]=='1')||(output[j+k]=='0'&&g[k]=='0'
))
output[j+k]='0';
else
output[j+k]='1';
for(j=0;j<output.length;j++)
{

if(output[j]=='1')
{
break;
}
}

res=new String(output);
if(mode==1)
System.out.println("crc code is "+p+" "+res.substring(output.length-
zero.length(), output.length));

return 1;
}

public static void main(String[] args) {

String r=null;
String i=null;
String o=null;

Scanner t;
System.out.println("\n Enter the message in Binary \n");
t=new Scanner(System.in);
i=t.nextLine();

crc(i,1);

System.out.println("\n Enter the recieved message \n");


t=new Scanner(System.in);
r=t.nextLine();

try{
if(crc(r,0)==1)
System.out.println("\n Error Free Message");

}catch(Exception e)
{
System.out.println("\n ERROR in the received
message....");
}

OUTPUTS

/* Output 1 */

Enter the message in Binary

1001

crc code is 1001 0100100010011001

Enter the recieved message

10010100100010011001

Error Free Message


/* Output 2 */

Enter the message in Binary

1001

crc code is 1001 0100100010011001

Enter the recieved message

ERROR in the received message....

You might also like