gpt4 book ai didi

java - Java中多项式x^16 + x^12 + x^5 + 1计算CCITT标准CRC

转载 作者:行者123 更新时间:2023-12-02 14:33:42 24 4
gpt4 key购买 nike

我需要在 Java 中使用多项式 x^16 + x^12 + x^5 + 1 (0x1081) 计算 CCITT 标准 CRC 的帮助。我在互联网上尝试了很多示例,但每个示例都返回与示例中不同的值。

例如,对于此数组 [0xFC] [05] [11],结果需要为 [27] [56]。

使用此代码:

public static void main(String[] args) {
byte[] array = new byte[3];
array[0] = (byte) 0xFC;
array[1] = (byte) 0x05;
array[2] = (byte) 0x11;
// array[3] = (byte) 0x00;
// array[4] = (byte) 0x00;

System.out.println(Integer.toHexString(crc16(array)));
}

private static final int POLYNOMIAL = 0x1081;
private static final int PRESET_VALUE = 0xFFFF;

public static int crc16(byte[] data) {
int current_crc_value = PRESET_VALUE;
for (int i = 0; i < data.length; i++) {
current_crc_value ^= data[i] & 0xFF;
for (int j = 0; j < 8; j++) {
if ((current_crc_value & 1) != 0) {
current_crc_value = (current_crc_value >>> 1) ^ POLYNOMIAL;
} else {
current_crc_value = current_crc_value >>> 1;
}
}
}
current_crc_value = ~current_crc_value;

return current_crc_value & 0xFFFF;
}

我得到的结果是FA DE,而不是[27] [56]

使用此代码:

public static void main(String[] args) { 
int crc = 0x0000;
int polynomial = 0x1081;

// byte[] testBytes = "123456789".getBytes("ASCII");

// byte[] array = args[0].getBytes();
byte[] array = new byte[3];
array[0] = (byte) 0xFC;
array[1] = (byte) 0x05;
array[2] = (byte) 0x11;

for (byte b : array) {
for (int i = 0; i < 8; i++) {
boolean bit = ((b >> (7-i) & 1) == 1);
boolean c15 = ((crc >> 15 & 1) == 1);
crc <<= 1;
if (c15 ^ bit) crc ^= polynomial;
}
}

crc &= 0xffff;
System.out.println("CRC16-CCITT = " + Integer.toHexString(crc));
}

我得到这个CRC16-CCITT = 8dca

使用此代码:

private final int polynomial = 0x1081;

private int[] table = new int[256];

public int ComputeChecksum(int[] bytes) {
int crc = 0xffff;
for (int i = 0; i < bytes.length; ++i) {
int index = (crc ^ bytes[i]) % 256;
crc = (crc >> 8) ^ table[index];
}
return crc;
}

public CRC162() {
int value;
int temp;
for (int i = 0; i < table.length; ++i) {
value = 0;
temp = i;
for (byte j = 0; j < 8; ++j) {
if (((value ^ temp) & 0x0001) != 0) {
value = (value >> 1) ^ polynomial;
} else {
value >>= 1;
}
temp >>= 1;
}
table[i] = value;
}
}

public static void main(String[] args) {
CRC162 c = new CRC162();
int[] arr = new int[]{0xFC, 0x05, 0x11};
System.out.println(Integer.toHexString(c.ComputeChecksum(arr)));
}

我得到这个521

希望有人能帮助我。我需要它来使用 ID003 协议(protocol)与设备进行通信。

编辑:使用 http://www.lammertbies.nl/comm/info/crc-calculation.html 上的在线计算器输入 FC0511,我从 CRC-CCITT (Kermit) 直接得到 0x2756。

最佳答案

x^16 + x^12 + x^5 + 1 不是 0x1081。它是0x1021。 x^5 是 20,而不是 80。(请注意,x^16 已被删除。)

此外,您需要的 Kermit CRC 已被反射(reflect),因此多项式被反转,给出 0x8408。

对于此 CRC,您用零进行初始化并且不对结果求补。

因此,相应地修改您的第一个示例,这将计算出您想要的内容:

public static void main(String[] args) {
byte[] array = new byte[3];
array[0] = (byte) 0xFC;
array[1] = (byte) 0x05;
array[2] = (byte) 0x11;
// array[3] = (byte) 0x00;
// array[4] = (byte) 0x00;

System.out.println(Integer.toHexString(crc16(array)));
}

private static final int POLYNOMIAL = 0x8408;
private static final int PRESET_VALUE = 0;

public static int crc16(byte[] data) {
int current_crc_value = PRESET_VALUE;
for (int i = 0; i < data.length; i++) {
current_crc_value ^= data[i] & 0xFF;
for (int j = 0; j < 8; j++) {
if ((current_crc_value & 1) != 0) {
current_crc_value = (current_crc_value >>> 1) ^ POLYNOMIAL;
} else {
current_crc_value = current_crc_value >>> 1;
}
}
}

return current_crc_value & 0xFFFF;
}

关于java - Java中多项式x^16 + x^12 + x^5 + 1计算CCITT标准CRC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24694713/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com