I want to convert a C code algorithm to Java to calculate a CRC. I am trying to implement the BL10 Concox GPS lock in our project, but I am not sure which byte[] should I pass to the CRC calculation function. Also I am not sure my implementation for the CRC calculation is correct.
I am passing byte[] from Packet Length to Information Serial Number as mentioned below.
Error check (From “Packet Length” to“Information Serial Number”) , are values of CRC-ITU. CRC error occur when the received information is calculated, the receiver will ignore and discard the data packet.
Here is the communication protocol pdf:
https://drive.google.com/file/d/1AsBk3iPyLGk4QyuDevYx86lJlfUVYE1t/view
Below is the C code.
C code:
static const U16 crctab16[] ={
0X0000, 0X1189, 0X2312, 0X329B, 0X4624, 0X57AD, 0X6536, 0X74BF,
0X8C48, 0X9DC1, 0XAF5A, 0XBED3, 0XCA6C, 0XDBE5, 0XE97E, 0XF8F7,
//remove some constant
0XF78F, 0XE606, 0XD49D, 0XC514, 0XB1AB, 0XA022, 0X92B9, 0X8330,
0X7BC7, 0X6A4E, 0X58D5, 0X495C, 0X3DE3, 0X2C6A, 0X1EF1, 0X0F78,
};
// calculate the 16-bit CRC of data with predetermined length.
U16 GetCrc16(const U8* pData, int nLength){
U16 fcs = 0xffff; // initialization
while(nLength>0){
fcs = (fcs >> 8) ^ crctab16[(fcs ^ *pData) & 0xff];
nLength--;
pData++;
}
return ~fcs; // negated
}
So far i have tried:
Java Code:
private static int crctab16[] = { 0X0000, 0X1189, 0X2312, 0X329B, 0X4624, 0X57AD, 0X6536, 0X74BF, 0X8C48, 0X9DC1,
0XAF5A, 0XBED3, 0XCA6C, 0XDBE5, 0XE97E, 0XF8F7, 0X1081, 0X0108, 0X6306, 0X728F, 0X4014, 0X519D, 0X2522,
0X34AB, 0X0630, 0X17B9, 0XEF4E, 0XFEC7, 0XCC5C, 0XDDD5, 0XA96A, ....
0X8B70, 0X8408, 0X9581, 0XA71A, 0XB693, 0XC22C, 0XD3A5, 0XE13E,
0XC60C, 0XD785, 0XE51E, 0XF497, 0X8028, 0X91A1, 0XA33A, 0XB2B3,
0XB1AB, 0XA022, 0X92B9, 0X8330, 0X7BC7, 0X6A4E, 0X58D5, 0X495C, 0X3DE3, 0X2C6A, 0X1EF1, 0X0F78, };
public static int GetCrc16(final byte[] pData) {
int crc = 0xffff;// initialization
for (byte b : pData) {
crc = (crc >>> 8) ^ crctab16[(crc ^ b) & 0xff];
}
return crc;
}
I checked the result using this, but not getting the correct result.
Your return crc;
needs to be return crc ^ 0xffff;
. Note the ~
in the return ~fcs;
from the C code. You didn't negate the result. It even provides the helpful comment "negated".
You could use ~crc
, but then you'd need to do ~crc & 0xffff
since int
s are 32 bits. So it's better to use crc ^ 0xffff
.
You do not need to use >>>
here. You can just use >>
since crc
will never be negative. Either one is fine.