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

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

Solution Checksum

Uploaded by

Subh Das
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 views4 pages

Solution Checksum

Uploaded by

Subh Das
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/ 4

Networking Lab

(PCC-CS-692)

Assignment for Checksum

1. Write a C code to encode a user provided dataword using checksum procedure.

Input:
Dataword: 11001100101010101111000011000011
Segment Length = 8

Output:
Checksum: 11010011
code word : 1100110010101010111100001100001111010011

#include <stdio.h>
#include <string.h>
#include <math.h>

int main() {

// *************START CODE FOR CHKSUM SENDER ************//


char data[100],data1[100];
int t,c,k,dl,sl,i,j=0,sum[100];
printf("\n enter the data \n");
scanf("%s",data);
dl=strlen(data);

//*** this done to mandet seglen must in pow of 2***********


while(1){
printf("\n enter the segment length \n");
scanf("%d",&sl);
for(i=1;i<=sl/2;i++)
{
if(pow(2,i)==sl)
{
j=1;
break;
}
}
if(j==1)
break;
else
printf("\n segment length must in pow of 2\n");
}
//*************************************************************

//if data length not divisable by segl adding 0 at front of data


if((dl%sl)!=0)
{
i=sl-(dl%sl);
for(j=0;j<i;j++)
data1[j]='0';
strcat(data1,data);
strcpy(data,data1);
dl=dl+i;
}
// ************

for(i=0;i<sl;i++)
sum[i]=0;

for(i=dl;i>0;i=i-sl)
{
c=0;
k=sl-1;
for(j=i-1;j>=i-sl;j--)
{
t=(sum[k]+(data[j]-48)+c);
sum[k]=t%2;
c=t/2;
k--;
}
if(c==1)
{
for(j=sl-1;j>=0;j--)
{
t=sum[j]+c;
sum[j]=t%2;
c=t/2;
}
}
}
printf("\n Checksum = ");
for(i=0;i<sl;i++){
if(sum[i]==0)
sum[i]=1;
else
sum[i]=0;
printf("%d",sum[i]);
data[dl+i]=sum[i]+48;

}
data[dl+i]='\0';

printf("\ncode word is: %s",data);


// *************END CODE FOR CHKSUM SENDER ************//

return(0);
}
2. Write a C code to decode a user provided codeword using Checksum procedure.
Provide the output along with original data (good/bad).

Input:
Codeword: 1100110010101010111100001100001111010011
Segment length: 8

Output:
Data recv ok
ACTUAL data IS: 1 1 0 0 1 1 0 0 1 0 1 0 1 0 1 0 1 1 1 1 0 0 0 0 1 1 0 0 0 0 1 1

#include <stdio.h>
#include <math.h>
#include <string.h>

int main() {
char data[100];
int dl,j,k,c,sum[100],t,sl,i;
printf("\n enter codeword:\n");
scanf("%s",data);
dl=strlen(data);

//*** this done to mandet seglen must in pow of 2***********


while(1){
printf("\n entr the segment length \n");
scanf("%d",&sl);
for(i=1;i<=sl;i++)
{
if(pow(2,i)==sl)
{
j=1;
break;
}
}
if(j==1)
break;
else
printf("\n segment length must in pow of 2\n");
}
//*************************************************************

for(i=0;i<sl;i++)
sum[i]=0;

for(i=dl;i>0;i=i-sl)
{
c=0;
k=sl-1;
for(j=i-1;j>=i-sl;j--)
{
t=(sum[k]+(data[j]-48)+c);
sum[k]=t%2;
c=t/2;
k--;

}
if(c==1)
{
for(j=sl-1;j>=0;j--)
{
t=sum[j]+c;
sum[j]=t%2;
c=t/2;
}
}
}
j=0;
for(i=0;i<sl;i++){
if(sum[i]!=1){
j++;break;}
}

if(j==0)
{
printf("\n data recv ok \n");
printf("\n ACTUAL data IS: \n");
for(i=0;i<dl-sl;i++)
printf("%c ",data[i]);
}
else
{
printf("\n data recv wrong \n");
}

return(0);
}

You might also like