- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要在 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/
我已经搜索过,但无法找到有关它是什么以及如何计算的信息。 我不知道为什么这个问题被否决了。不是说清楚和编程有关吗?或者我应该问: # Or you can compute the running CR
如果我有一定数量的字节要串行传输,我如何确定使用哪个 CRC(CRC8、CRC16 等,基本上是多少位的 CRC?)并且错误检测百分比仍然很高?有这个公式吗? 最佳答案 从 CRC 长度的角度来看,适
这个问题在这里已经有了答案: Incremental Checksums (3 个答案) 关闭 8 年前。 如果我有子串 S0, S1, ... Sn 计算出的 CRCs C0, C1, ... C
我知道 CRC 是一个线性函数,这意味着 CRC(x xor y) = CRC(x) xor CRC(y),但我不知道如何证明 CRC 的这个属性。 有谁有想法吗? 非常感谢! 最佳答案 这通常不是真
我知道使用 CRC 的全部目的是进行错误检测,但我听说有人说除了错误检测之外,它还可以用来进行基本的错误纠正。我很好奇是否是这样,如果是这样,它有多强大?我的意思是,我们通常将 CRC 称为能够执行
我见过 8 位、16 位和 32 位 CRC。 在什么时候我需要跳转到更广泛的 CRC? 我的直觉 react 是它基于数据长度: 1-100 字节:8 位 CRC 101 - 1000 字节:16
我正在学习 CRC,搜索引擎和 SO 对此一无所知...... 为什么我们有“正态”、“逆向”和“倒数”多项式?是喜欢 Big Endian、Little Endian 还是其他什么? 最佳答案 CR
在学习计算机网络类(class)时,教授谈到了示例代码中两个有效代码字之间的汉明距离。我已经阅读了有关汉明距离的信息,从告诉 2 个字符串之间的差异距离的角度来看,这是有道理的。例如: Code Wo
我从Wikipedia知道CRC计算算法。关于RAR文件的结构我读过here 。例如有这样写: The file has the magic number of: 0x 52 61 72 21
我已经阅读了 CCITT 和 TI 关于 msp430 的文档。是否可以使用任何内置函数计算 MSP430F5438A 的 CRC?或者我是否必须为每个获取的数据计算 CRC。 最佳答案 可以使用软件
我尝试修改现有项目的 CRC 模块。目前它使用 CRC-16-CCITT。从代码中我知道它的多项式是0x11021,它的CRC-Table是: static const unsigned sh
使用线性反馈移位寄存器(LFSR)实现CRC生成有两种方法,如图所示。图中生成多项式的系数为100111,红色“+”圆圈为异或运算符。两者的初始化寄存器值都是 00000。 例如,如果输入数据比特流是
当数据长度不是4的倍数时,有没有办法通过表驱动的方式加快CRC校验过程?在我的问题中,有 226 位需要检查,并通过 CRC24Q 算法为此计算了 24 位校验和。 最佳答案 使用表格计算第一部分的c
我需要计算消息的 CRC 并将其放在该消息的开头,以便带有“前置”补丁字节的消息的最终 CRC 等于 0。我能够做到这在几篇文章的帮助下非常容易,但不适用于我的特定参数。问题是我必须使用给定的 CRC
我正在将一些从我自己的类中定义的对象保存到文件中。 (保存流数据)。 这一切都很好,但我希望能够在文件中存储该文件的 CRC 校验和。 然后,每当我的应用程序尝试打开文件时,它都可以读取内部存储的 C
我有一组受 16 位校验和保护的数据,需要更正。校验和位置是已知的,计算它们的确切区域以及用于计算它们的确切算法却未知。 16 位,最低有效位在前。我怀疑这是某种 16 位 CRC,但我无法找到实际计
我想使用Nucleo L053R8的crc计算单元计算3字节的CRC值。生成多项式如下:g(X)=x^24 + x^10 + x^9 + x^6 + x^4 + x^3 + x + 1 看来使用这个C
开始之前:我绝对是 JAVA 的初学者。我一直是一名 C++ 程序员。所以当我在这里做傻事时请告诉我! 我正在查询一个巨大的数据库并将该数据直接导出到一个 zip 文件中。我们在这里讨论的是 35GB
我有一些 C++ 代码,我正在将其移植到 C。当我在 C 代码中计算 CRC 时,出于某种原因,它返回错误的 CRC 值,而 C++ 代码运行良好。我是 C++ 的新手。我需要一些帮助来理解我在返回错
我有两个来源来计算看似相同的 crc 值。我不明白为什么“boost/crc.hpp”实现与“linux/lib/crc-ccitt.c”实现不同。 crc-ccitt.c boost 这是一个说明问
我是一名优秀的程序员,十分优秀!