DecodingUtils { public static int getCrc16(byte[] bytes, int offset, int length) { int contentSize = length - offset; byte[] content = new byte[contentSize]; System.arraycopy(bytes, offset, content, 0, contentSize); int value = 0; for (byte element : content) { value = unsignedShort((value << 8) ^ CRC16_TABLE[((value >>> 8) ^ element) & 0xff]); } return value; }  static int getCrc16(byte[] bytes, int offset, int length); static int unsignedShort(int value); static void validateCrc(int receiverCrc, byte[] content); static byte[] fromHexString(String hexString); static String toHexString(byte[] value); static final int CRC16_SIZE; }
@Test public void crc16CalculationShouldMatchLastTwoBytesOfPacket() throws Exception { int crc16 = DecodingUtils.getCrc16(this.packet, 0, this.packet.length - 2); assertThat(format("CRC16 validation for packet [%s] failed", this.commandId), (short) crc16, equalTo(getPacketCrcValue())); }