具有十六进制长数组数据类型java的CRC16
我正试图通过使用以下代码获得正确的CRC16具有十六进制长数组数据类型java的CRC16,java,crc16,Java,Crc16,我正试图通过使用以下代码获得正确的CRC16 public static int GenCrc16(final byte[] buffer) { int crc = 0xffff; for (int j = 0; j < buffer.length ; j++) { crc = ((crc >>> 8) | (crc << 8) )& 0xffff; crc ^= (buffer[j] & 0xff);//
public static int GenCrc16(final byte[] buffer) {
int crc = 0xffff;
for (int j = 0; j < buffer.length ; j++) {
crc = ((crc >>> 8) | (crc << 8) )& 0xffff;
crc ^= (buffer[j] & 0xff);//byte to int, trunc sign
crc ^= ((crc & 0xff) >> 4);
crc ^= (crc << 12) & 0xffff;
crc ^= ((crc & 0xFF) << 5) & 0xffff;
}
crc &= 0xffff;
return crc;
}
我得到了0x17B5,这是正确的,但我希望将数据作为
作为原始数据
long[] inputLongArray= {0x00000140, 0x00000000, 0x00000002, 0x00000001}
public static byte[] toByte(long[] longArray) {
ByteBuffer bb = ByteBuffer.allocate(longArray.length * Long.BYTES);
bb.asLongBuffer().put(longArray);
return bb.array();
}
我期待0x1F19,如下所示
00000140 00000000 00000002 00000001并选择十六进制数据类型和CRC-16/CCITT-FALSE
public static void main(String[] args) {
long[] intputarray = {0x00000140, 0x0000000, 0x000000002, 0x0000001};
System.out.println(Integer.toHexString(GenCrc16(toByte(intputarray)));
}
我做错了什么。提前谢谢你的帮助
long
值是8个字节,也就是16个十六进制数字,所以long[]{0x00000140,0x00000000,0x00000002,0x00000001}
与
“000000000000014000000000000000000000000000000000000000000020000000000000001”
并且都具有CRC-16/CCITT-FALSE值
字符串
“000001400000000000000020000001”
应该是
“0000014000000000000002000000001”
这与
int[]{0x00000140,0x00000000,0x00000002,0x00000001}
并且都具有CRC-16/CCITT-FALSE值
public static void main(String[] args) {
long[] intputarray = {0x00000140, 0x0000000, 0x000000002, 0x0000001};
System.out.println(Integer.toHexString(GenCrc16(toByte(intputarray)));
}
public static byte[] toByte(final int... intArray) {
ByteBuffer bb = ByteBuffer.allocate(intArray.length * Integer.BYTES);
bb.asIntBuffer().put(intArray);
return bb.array();
}