// function to calculate CRC-16
UInt16 CRC(void * data, UInt32 len)
{
UInt8 * dataPtr = (UInt8 *)data;
UInt32 index = 0;
// Update the CRC for transmitted and received data using
// the CCITT 16bit algorithm (X^16 + X^12 + X^5 + 1).
UInt16 crc = 0;
while(len--)
{
crc = (unsigned char)(crc >> 8) | (crc << 8);
crc ^= dataPtr[index++];
crc ^= (unsigned char)(crc & 0xff) >> 4;
crc ^= (crc << 8) << 4;
crc ^= ((crc & 0xff) << 4) << 1;
}
return crc;
}
------解决思路----------------------
刚才有点问题,改成下面的:
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public short CRC(byte[] arr, int len) {
int index = 0;
// Update the CRC for transmitted and received data using
// the CCITT 16bit algorithm (X^16 + X^12 + X^5 + 1).
short crc = 0;
while (index < len) {
crc = (short) ((crc >> 8)
------解决思路----------------------
(crc << 8));
crc ^= arr[index++];
crc ^= (char) (crc & 0xff) >> 4;
crc ^= (crc << 8) << 4;
crc ^= ((crc & 0xff) << 4) << 1;
}
return crc;
}