byte数组计算校验和,帧头长度为12

 
private boolean matchCrc(byte[] readData, int packLength, intindex) {
int total = 0;
int x = 0;

// 计算校验和,这里帧头长度为12
for (x = index + 12; x < index + 12 + packLength &&x < readData.length; x++) {

total += readData[x];
if (readData[x] < 0) {
// System.out.println(readData[x] + "<0");
total += 256;
}
}
if ((index + 13 + packLength) < readData.length) {
if (readData[index + 12 + packLength] == (byte) ((total &0xff00) >> 8)
&& (readData[index + 13 + packLength] == (byte) (total& 0xff))) {
return true;
}
}
return false;
}


1 2 3 4 5 6 7 8 9 10 11 12
23+3/43+3 24+3/44+3
数据包(11+字节)
帧头     IP地址 包长(数据长度) 数据 校验和
0x01 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF -- -- …… -- --
                    包长后加起        

你可能感兴趣的:(Android其他)