Computer Network: CheckSum in C Programming

Posted on:
tags: , ,

A checksum or hash sum is a fixed-size datum computed from an arbitrary block of digital data for the purpose of detecting accidental errorsthat may have been introduced during its transmission or storage. The integrity of the data can be checked at any later time by recomputing the checksum and comparing it with the stored one. If the checksums match, the data was almost certainly not altered.






#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int r,t[20],x,o,a[30],i,j,k,z,aa,b[20],n,y,s,n1,flag=0;
clrscr();
printf("*********sender side***********\n");
printf("enter the data length:");
scanf("%d",&n);
for(i=1;i<=30;i++)
{
a[i]=0;
}
printf("enter the data:");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("enter the chopping size:");
scanf("%d",&o);
z=0;
for(i=1;i<=n;i=i+o)
{
x=o-1;
k=0;
for(j=i;j<i+o;j++)
{
k+=pow(2,x)*a[j];
x--;
}
z=z+k;
}
printf("\ntotal in decimal is=%d",z);
y=0;
s=1;
while(z>=1)
{
aa=z%2;
t[s]=aa;
s++;
z=z/2;
y++;
}
if(y>o)
{
t[1]=t[o+1]+t[1];
}
r=1;
for(i=o;i>=1;i--)
{
b[r]=t[i];
r++;
}
for(i=o;i>=1;i--)
{
if(b[i]>1)
{
b[i]=b[i]%2;
b[i-1]+=1;
}
}
printf("\nb[i]=");
for(i=1;i<=o;i++)
printf("\t%d",b[i]);


for(i=1;i<=o;i++)
{
if(b[i]==0)
b[i]=1;
else
b[i]=0;
}
printf("\nthe checksum value is:");
for(i=1;i<=o;i++)
{
printf("\t%d",b[i]);
}
printf("\n*********Receiver side*********\n");
for(i=1;i<=30;i++)
{
a[i]=0;
}
n=n+o;
printf("enter the data:");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
z=0;
for(i=1;i<=n;i=i+o)
{
x=o-1;
k=0;
for(j=i;j<i+o;j++)
{
k+=pow(2,x)*a[j];
x--;
}
z=z+k;
}
printf("total in decimal is:%d",z);
y=0;
s=1;
while(z>=1)
{
aa=z%2;
t[s]=aa;
s++;
z=z/2;
y++;
}
if(y>o)
{
t[1]=t[o+1]+t[1];
}
r=1;
for(i=o;i>=1;i--)
{
b[r]=t[i];
r++;
}
for(i=o;i>=1;i--)
{
if(b[i]>1)
{
b[i]=b[i]%2;
b[i-1]+=1;
}
}
printf("\nb[i]=");
for(i=1;i<=o;i++)
printf("\t%d",b[i]);
for(i=1;i<=o;i++)
{
if(b[i]==0)
b[i]=1;
else
b[i]=0;
}
printf("\nthe checksum value is:");
for(i=1;i<=o;i++)
{
printf("\t%d",b[i]);
}
for(i=1;i<=o;i++)
{
if(b[i]==1)
{
flag=1;
}
}
if(flag!=0)
printf("\nError present in the code....");
else
{
printf("\nNO error present in the code....");
getch();
printf("\n\tcode successfully accepted by receiver.....");
}
getch();


}


to know more about it :


http://en.wikipedia.org/wiki/Check_sum

No comments:

Post a Comment

< >