十六进制长数组数据类型 java 的 CRC16
CRC16 with hex long array Data type java
我正在尝试使用以下代码获得正确的 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);//byte to int, trunc sign
crc ^= ((crc & 0xff) >> 4);
crc ^= (crc << 12) & 0xffff;
crc ^= ((crc & 0xFF) << 5) & 0xffff;
}
crc &= 0xffff;
return crc;
}
如果我传递一个字符串,
String input = "00000140 0000000 000000002 0000001";
我得到 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 按照 https://crccalc.com 和以下
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}
与
相同
"0000000000000140 0000000000000000 0000000000000002 0000000000000001"
两者都有 CRC-16/CCITT-FALSE 值
E47B
字符串
"00000140 0000000 000000002 0000001"
真的应该
"00000140 00000000 00000002 00000001"
与
相同
int[] {0x00000140, 0x00000000, 0x00000002, 0x00000001}
两者都有 CRC-16/CCITT-FALSE 值
1F19
public static byte[] toByte(final int... intArray) {
ByteBuffer bb = ByteBuffer.allocate(intArray.length * Integer.BYTES);
bb.asIntBuffer().put(intArray);
return bb.array();
}
我正在尝试使用以下代码获得正确的 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);//byte to int, trunc sign
crc ^= ((crc & 0xff) >> 4);
crc ^= (crc << 12) & 0xffff;
crc ^= ((crc & 0xFF) << 5) & 0xffff;
}
crc &= 0xffff;
return crc;
}
如果我传递一个字符串,
String input = "00000140 0000000 000000002 0000001";
我得到 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 按照 https://crccalc.com 和以下 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}
与
相同
"0000000000000140 0000000000000000 0000000000000002 0000000000000001"
两者都有 CRC-16/CCITT-FALSE 值
E47B
字符串
"00000140 0000000 000000002 0000001"
真的应该
"00000140 00000000 00000002 00000001"
与
相同
int[] {0x00000140, 0x00000000, 0x00000002, 0x00000001}
两者都有 CRC-16/CCITT-FALSE 值
1F19
public static byte[] toByte(final int... intArray) {
ByteBuffer bb = ByteBuffer.allocate(intArray.length * Integer.BYTES);
bb.asIntBuffer().put(intArray);
return bb.array();
}