如何在 swift 中实现 CRC-CCITT?我设法让它在 Java 上运行
public static String createCRC(String string) {
String crcCode = "";
int crc = 0xFFFF; // initial value
int polynomial = 0x1021; // 0001 0000 0010 0001 (0, 5, 12)
// byte[] testBytes = "123456789".getBytes("ASCII");
byte[] bytes = string.getBytes();
for (byte b : bytes) {
for (int i = 0; i < 8; i++) {
boolean bit = ((b >> (7-i) & 1) == 1);
boolean c15 = ((crc >> 15 & 1) == 1); …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 Ruby 和 Python 中为相同的字符串生成 CRC32 校验和并获得不同的结果。
Python
from zlib import crc32
x = "SheetJS"
crc32(x)
#=> -1647298270
Run Code Online (Sandbox Code Playgroud)
节点
var CRC32 = require('crc-32');
var x = "SheetJS";
CRC32.str(x);
#=> -1647298270
Run Code Online (Sandbox Code Playgroud)
红宝石
require 'zlib'
x = "SheetJS"
Zlib::crc32(x)
#=> 2647669026
Run Code Online (Sandbox Code Playgroud) 我有以下代码(手动版本来自阿德勒的答案)
#include <iostream>
#include <nmmintrin.h>
#define POLY2 0x82f63b78
uint32_t crc32c2(uint32_t crc, const unsigned char *buf, size_t len)
{
int k;
crc = ~crc;
while (len--) {
crc ^= *buf++;
for (k = 0; k < 8; k++)
crc = crc & 1 ? (crc >> 1) ^ POLY2 : crc >> 1;
}
return ~crc;
}
int main(int argc, char **argv)
{
const unsigned int val = 5;
std::cout << std::hex << crc32c2(0,(const unsigned char*)&val,4) << std::endl;
std::cout …Run Code Online (Sandbox Code Playgroud) 我遇到了一个据称非常高效和优雅的 CRC 实现,我试图真正理解所有步骤。我了解迭代每一位的 CRC-CCITT 0x1021 实现,但我正在努力获得这个。这是代码。
/*
* Original Code by Ashley Roll
*/
uint16_t crc16(uint8_t* data, uint16_t length){
uint8_t x;
uint16_t crc = 0xFFFF;
while (length--){
x = crc >> 8 ^ *data++;
x ^= x>>4;
crc = (crc << 8) ^ ((uint16_t)(x << 12)) ^ ((uint16_t)(x <<5)) ^ ((uint16_t)x);
}
return crc;
}
Run Code Online (Sandbox Code Playgroud)
谁能更深入地向我解释一下 x 变量发生了什么?这就是我迄今为止能够掌握的
x = crc >> 8 ^ *data++; // Here, we take the high byte of the register
// and we XOR it …Run Code Online (Sandbox Code Playgroud) 我正在做一个模糊媒体播放器的项目.我用Java编写了文件生成器,并用C编写的原始压缩代码转换了CRC生成器.我可以用DataOutputStream编写数据,但我无法弄清楚如何在java中将数据作为无符号字符数组发送.在C中,这是一个非常简单的过程.我已经非常彻底地搜索了一个解决方案,我发现最好的解决方案就是将数据发送到C并让C返回CRC.我可能只是没有正确搜索,因为我对这些东西很不熟悉.非常感谢您的帮助.
我想为给定的文件列表生成并存储CRC(或类似)值,这些值可以在以后用作比较.编写一个函数来做到这一点很简单,但是在Python库中有更标准的方法吗?
生成的值不需要具有任何特定标准.
我正在尝试使用数据字节创建CRC,我已经编写了这个函数:
u16 crcGenerate(unsigned char bytes, int len){
u16 crc = 0;
for (int i = 0; i < len; i++){
crc = crc16__computeByteAnsi(crc, bytes[i]); }
return crc;
}
Run Code Online (Sandbox Code Playgroud)
每当调用此函数时,我都会遇到上述错误,我不太确定如何解决它,甚至是什么问题.
为了避免这个问题膨胀,我已经包含了引用的其他主要.h和.cpp文件.
我非常感谢能得到的任何帮助; 如果还有其他需要提供的信息,请告诉我.我对C/C++不是很熟悉,我不知道是什么导致了这个问题.
谢谢.
我过去与CRC-16校验和有过联系,习惯于通过对要验证的文件以及CRC-16本身的2个字节重新计算CRC-16校验和来进行验证。如果结果为零,则文件完整性有效,否则无效。
可以像下面的伪C一样非常有效地进行编码:
if (recalculated_crc16_checksum != 0)
// Error: file integrity is corrupt
else
// Success: file integrity is valid
我最近想使用CRC-32校验和进行文件完整性检查,并尝试以相同的方式进行验证,但似乎这里“ Compare-Against-Zero-Trick”是不可能的?
例如,如果我在CRC在线计算器上使用32位值0xDEADBEEF:
CRC-16-Modbus(0xDEADBEEF) = 0xC19B
(Same input value but with appended checksum 0xC19B in reversed byte ordering)
CRC-16-Modbus(0xDEADBEEF9BC1) = 0x0000
但:
CRC-32(0xDEADBEEF) = 0x7C9CA35A
(I tried both: big and little endian byte ordering for the appended checksum)
CRC-32(0xDEADBEEF7C9CA35A) = 0x01F92292 != 0x00000000
CRC-32(0xDEADBEEF5AA39C7C) = 0x2144DF1C != 0x00000000
有人可以向我解释一下,为什么此“ Compare-Against-Zero-Trick”不适用于CRC-32吗?