#include <stdio.h>
/* 校验和函数 */
int checksum(const char *arr, int arrsize)
{
int i = 0, sum = 0;
for (; i < arrsize; i ++)
sum += (unsigned char)arr[i];
if (sum > 0xff)
{
sum = ~sum;
sum += 1;
}
sum &= 0xff;
return sum;
}
int main(void)
{
char str[] = "hello, world\n";
printf("0x%x\n", checksum(str, sizeof(str)));
return 0;
}
追问谢谢大侠。很好的完成了总和的值。