概述
checksum.c
#include "stdio.h"
#include "stdlib.h"
/**
* @brief CheckSum8计算
* @param cksum : 计算结果初始值,一般为0
* @param pBuffer : 数据缓存
* @param size : 数据长度
* @return CheckSum8计算结果
*/
unsigned short CheckSum8(unsigned int cksum, void *pBuffer, int size)
{
char num = 0;
unsigned char *p = (unsigned char *)pBuffer;
if ((NULL == pBuffer) || (0 == size))
{
return cksum;
}
while (size > 1)
{
cksum += ((unsigned short)p[num] << 8 & 0xff00) | (unsigned short)p[num + 1] & 0x00FF;
size -= 2;
num += 2;
}
if (size > 0)
{
cksum += ((unsigned short)p[num] << 8) & 0xFFFF;
num += 1;
}
while (cksum >> 16)
{
cksum = (cksum & 0xFFFF) + (cksum >> 16);
}
return ~cksum;
}
/**
* @brief CheckSum16计算
* @param pBuffer:数据缓存
* @param len:数据长度
* @return CheckSum16计算结果
*/
unsigned short CheckSum16(unsigned short *pBuffer, int len)
{
unsigned short chksum;
unsigned int sum = 0;
while(len > 1)
{
sum += *pBuffer++;
len -= 2;
}
if(len == 1)
sum += *(unsigned char*)pBuffer;
sum = (sum>>16) + (sum & 0xffff);
sum += (sum>>16);
chksum = ~sum;
return (chksum);
}
checksum.h
#ifndef __CHECKSUM_H__
#define __CHECKSUM_H__
/**
* @brief CheckSum8计算
* @param cksum : 计算结果初始值,一般为0
* @param pBuffer : 数据缓存
* @param size : 数据长度
* @return CheckSum8计算结果
*/
unsigned short CheckSum8(unsigned int cksum, void *pBuffer, int size);
/**
* @brief CheckSum16计算
* @param pBuffer:数据缓存
* @param len:数据长度
* @return CheckSum16计算结果
*/
unsigned short CheckSum16(unsigned short *pBuffer, int len);
#endif //__CHECKSUM_H__
main.c
#include "stdio.h"
#include "stdlib.h"
#include "checksum.h"
unsigned char _data8 [] = {
0x00, 0x00, // checksum result
0x11, 0x12,
0x13, 0x14,
0x15, 0x14,
0x00, 0x02,
0x11, 0x12,
0x13, 0x14,
0x15, 0x14,
0xff, 0x02,
0x11, 0x12,
0x13, 0x14,
0xff, 0x14,
0x11, 0x12,
0x13, 0xcc,
0x15, 0x14,
0x00, 0x02,
0x11, 0x12,
0x13, 0x14,
0x15, 0xdd,
0x00, 0x02,
0x11, 0x1f,
0x13, 0x14,
0x15, 0x14,
0x11, 0x12,
0x13, 0x14,
0x15, 0x14,
0x00, 0x02,
0x11, 0x12,
0x13, 0x14,
0x19, 0x14,
0x00, 0x02,
0x11, 0x12,
0xf3, 0x14,
0xff
};
unsigned short _data16 [] = {
0x0000, // checksum result
0x1238,
0x2039,
0x2384,
0xaf92,
0x2848,
0xbc38,
0x2039,
0x2384,
0xaf92,
0x2848,
0x1238,
0xa839,
0xff84,
0xaf92,
0x2848
};
int main(int argc, char**argv)
{
unsigned short chksum;
printf("CheckSum8 demo.n");
chksum = CheckSum8(0, _data8, sizeof(_data8));
printf("CheckSum8 :0x%04xn", chksum);
_data8[0] = chksum >> 8;
_data8[1] = chksum & 0xff;
//_data8[sizeof(_data8)-2] = chksum >> 8;
//_data8[sizeof(_data8)-1] = chksum & 0xff;
chksum = CheckSum8(0, _data8, sizeof(_data8));
if (chksum != 0) {
printf("CheckSum8 incorrect :0x%04x.n", chksum);
} else {
printf("CheckSum8 correct :0x%04x.n", chksum);
}
printf("nCheckSum16 demo.n");
chksum = CheckSum16(_data16, sizeof(_data16));
printf("CheckSum16:0x%04xn", chksum);
_data16[0] = chksum;
//_data16[sizeof(_data16)/sizeof(unsigned short) - 1] = chksum;
chksum = CheckSum16(_data16, sizeof(_data16));
if (chksum != 0) {
printf("CheckSum16 incorrect :0x%04x.n", chksum);
} else {
printf("CheckSum16 correct :0x%04x.n", chksum);
}
return 0;
}
最后
以上就是不安斑马为你收集整理的checksum 校验和checksum.cchecksum.hmain.c的全部内容,希望文章能够帮你解决checksum 校验和checksum.cchecksum.hmain.c所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复