checksum

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


char test[128] =
{0x33,0x30,0x46,0x4e,0x54,0x53,0x53,0x46,0x44,0x38,
0x44,0x20,0x20,0x20,0x57,0x4f,0x46,0x46,0x41,0x44,
0x44,0x42,0x41,0x41,0x33,0x4b,0x43,0x31,0x39,0x33,
0x34,0x39,0x41,0x44,0x41,0x41,0x30,0x31,0x2d,0x2d,
0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,
0x2d,0x2d,0x52,0x54,0x20,0x20,0x52,0x54,0x31,0x34,
0x32,0x30,0x30,0x30,0x31,0x33,0x36,0x20,0x20,0x20,
0x20,0x20,0x20,0x20,0x30,0x30,0x31,0x34,0x30,0x35,
0x31,0x33,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,
0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,
0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,
0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,
0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0xe6,0xca};



typedef unsigned char      U8;    
typedef unsigned short int U16;

U16 chkSum(unsigned char *pucBuf, int iLen)
{
        unsigned short  usChkSum;
        int             iIndex;

        usChkSum = 0;
        for (iIndex=0; iIndex<iLen; iIndex++){
                usChkSum += *pucBuf++;
        }

        usChkSum = ~usChkSum + 1;
        return usChkSum;
}

/* Function: flip
** Remarks:  flip checksum bits 
*/
U16 flip(unsigned short usChkSum)
{
        return (((usChkSum >> 8) & 0xff) + ((usChkSum & 0xff) << 8));
}



int main()
{
        U16 checkSum = chkSum((U8*)test,126);

        U16 flipSum = flip(checkSum);

        printf("checksum =0x%x, flipSum =0x%x\n", checkSum, flipSum);

        return 0;


你可能感兴趣的:(SUM,check)