Compute Checksum Function

unsigned short ComputeChecksum(unsigned short *data, int size)
{
    register int nleft=size;
    unsigned long checksum = 0;
    while(nleft>1)
    {
        checksum=checksum+*data++;
        nleft=nleft-sizeof(unsigned short);
    }
    if(nleft)
        checksum=checksum+*(unsigned char*)data;
    checksum=(checksum>>16)+(checksum&0xffff);
    checksum=checksum+(checksum>>16);
    return (unsigned short)(~checksum);
}

You can translate above code in any language (either Java or C#). It was written in C language.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s