<span style= "White-space:pre" ></span>/** * calculates the generated check code * * @param data * needs to be validated * @return */public St atic String MAKE_CRC (byte[] data) {byte[] buf = new byte[data.length];//stores the data for which the checksum needs to be generated for (int i = 0; i < data.length; I + +) {Buf[i] = data[i];} int len = buf.length;int CRC = 0xffff;for (int pos = 0; pos < len; pos++) {if (Buf[pos] < 0) {CRC ^= (int) Buf[pos] + 256; XOR byte into least sig. Byte of//CRC} else {CRC ^= (int.) Buf[pos];//XOR byte into least sig. Byte of crc}for (int i = 8; I! = 0; i--) {//Loop over each bitif ((CRC & 0x0001)! = 0) {//If the LSB is SETCRC >>= 1;//Shift Right and XOR 0xA 001CRC ^= 0xa001;} else//Else LSB is not SETCRC >>= 1; Just shift right}}string C = integer.tohexstring (CRC); if (c.length () = = 4) {c = c.substring (2, 4) + c.substring (0, 2);} else if (c.length () = = 3) {c = "0" + c;c = c.substring (2, 4) + c.substring (0, 2);} else if (c.length () = = 2) {c = "0" + C . substring (1, 2) + "0" + c.substring (0,1);} return c;}
Java version CRC check