C 语言校验和算法
/**
* 校验和算法
* @src 校验字符串首地址
* @sizes 总字节数
* @return 一个字节的校验和
*/
unsigned char checkSum(const char *src, unsigned long sizes) {
unsigned char ret = 0;
unsigned long i = 0;
unsigned int sum = 0;
const char *ptr = NULL;
ptr = src;
if (ptr != NULL) for (i = 0; i < sizes; i++) {
sum += ((unsigned char*)ptr)[i];
}
ret = sum & 0xff;
return ret;
}
JAVA 语言校验和算法
/**
* vggbmmm 校验和算法
*
* @src 校验字符串首地址
* @sizes 总字节数
* @return 一个字节的校验和
*/
public static char checkSum(char[] src) {
int sum = 0;
char ret = 0;
if (src != null) {
for (int i = 0; i < src.length; i++) {
sum += (char) src[i];
}
}
ret = (char) (sum & 0xff);
return ret;
}