#include "windows.h"
#include <string>
#include <iostream>
unsigned long crc32_table[] = {
0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419,
0x706AF48F, 0xE963A535, 0x9E6495A3, 0x0EDB8832, 0x79DCB8A4,
0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07,
0x90BF1D91, 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE,
0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7, 0x136C9856,
0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9,
0xFA0F3D63, 0x8D080DF5, 0x3B6E20C8, 0x4C69105E, 0xD56041E4,
0xA2677172, 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B,
0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3,
0x45DF5C75, 0xDCD60DCF, 0xABD13D59, 0x26D930AC, 0x51DE003A,
0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423, 0xCFBA9599,
0xB8BDA50F, 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924,
0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D, 0x76DC4190,
0x01DB7106, 0x98D220BC, 0xEFD5102A, 0x71B18589, …Run Code Online (Sandbox Code Playgroud) 我有一个带有 C CRC 算法计算的从站 modbus 板,我必须在 Android 应用程序上使用 Java 中的主站使用相同的功能。
C函数是这样的:
unsigned char ucCRCHi = 0xFF;
unsigned char ucCRCLo = 0xFF;
unsigned short iIndex;
while( len-- ){
iIndex = ucCRCLo ^ *( ptr++ );
ucCRCLo = ( unsigned char )( ucCRCHi ^ aucCRCHi[iIndex] );
ucCRCHi = aucCRCLo[iIndex];
}
return ( unsigned short )( ucCRCHi * 256 + ucCRCLo );
Run Code Online (Sandbox Code Playgroud)
我尝试过这个Java代码:
public int calc(byte[] buffer, int usDataLen)
{
int uchCRCHi = 0xff;
int uchCRCLo = 0xff;
int uIndex;
int i = …Run Code Online (Sandbox Code Playgroud) 如何计算32位循环冗余校验(CRC-32)作为PostgreSQL中的函数,与MySQL一样?
我试图理解(并随后实现)干净飞行四轴飞行器固件用于 srxl 包验证的 crc16 验证。
SRXL 是一种简单的串行协议,用于通过单条串行线传输伺服值。
包结构是这样的:
crc16 通过以下函数验证:
//srxlFrameLength is the package length
//srxlFrame is the received package buffer
uint16_t crc_calc = 0;
for (i = 0; i < srxlFrameLength; i++) {
crc_calc = crc16_CCITT(crc_calc, srxlFrame[i]);
}
if(crcCalc == 0){ //package is valid }
Run Code Online (Sandbox Code Playgroud)
我不明白这是如何运作的。如果我要实现这个,我会迭代计算包的所有字节的 crc,直到达到保存的 crc,然后与保存的 crc 进行比较。为什么这个实现也有效?
我还想为发送方实现一个包生成器代码。是否可以像这样实现 crc 生成:
uint16_t crc_calc = 0;
for(int i = 0; i < packetLength; ++i){
crc_calc = crc16_CCITT(crc_calc, packet[i]);
}
//concat calculated crc16 to packet here.
Run Code Online (Sandbox Code Playgroud)
提前致谢,
马尔特
我正在尝试修改的一款旧游戏有 CRC-32 检查。我无权访问 CRC 校验和本身,因此我必须修改文件,同时保持相同的校验和输出。
我有的信息
文件,如果文件本身是CRC-32程序的唯一输入,我可以计算出CRC-32校验和应该是什么。
32 位多项式代码。
我使用 C zlib API 是因为它具有将crc32_combine校验和连接在一起的功能,而 Boost 则没有。
但是,我需要使用 polynomial 来实现 CRC32-C (Castagnoli) 校验和0x1EDC6F41,而不是标准的 CRC32 校验和。
使用 Boost 我显然可以使用:
#include <boost/crc.hpp>
using crc_32c_type = boost::crc_optimal<32, 0x1EDC6F41, 0xFFFFFFFF, 0xFFFFFFFF, true, true>;
crc_32c_type result;
result.process_bytes(reinterpret_cast<const char*>(&buffer), len);
return result.checksum();
Run Code Online (Sandbox Code Playgroud)
其中可以使用0x1EDC6F41多项式。
有没有类似的方法可以让我用 zlib 做到这一点?
我需要使用 Python 计算此 CRC 以与 Aurora (ABB) 太阳能逆变器通信。
这是文档:http : //www.drhack.it/images/PDF/AuroraCommunicationProtocol_4_2.pdf 在最后一页中有计算 CRC 的说明,我需要在 python 中进行。
我得到的消息是
MESSAGE_GRID_VOLTAGE = bytes.fromhex("023b010000000000")
Run Code Online (Sandbox Code Playgroud)
结果应该是:
CRC_L = FF
CRC_H = 2C
然后我需要像这样发送带有 CRC 的完整消息:
MESSAGE_GRID_VOLTAGE = bytes.fromhex("023b010000000000ff2c")
Run Code Online (Sandbox Code Playgroud)
我怎么能在python中做到这一点?谢谢!
这是我尝试过的代码:
message = "023b010000000000"
BccLo= int ("FF",16)
BccHi= int("FF", 16)
New = int(message, 16)
New = New ^ BccLo
Tmp=New << 4
New=Tmp ^ New
Tmp=New >> 5
BccLo=BccHi
BccHi= New ^ Tmp
Tmp=New << 3
BccLo=BccLo ^ Tmp
Tmp=New >> 4
BccLo=BccLo ^ Tmp …Run Code Online (Sandbox Code Playgroud) 事实证明,CRC32C 比 CRC32 提供更好的结果(改进的汉明距离和更快的实现)。为什么以太网仍然使用旧的 CRC 32 而不是 CRC32C?
我试图找到种子来散列最大可能长度的小写字母短字符串而不会发生冲突。我选择了 SSE 4.2 CRC32 来简化任务。对于长度为 4、5、6 的种子,在一些合理的小值内没有碰撞(我不能无限等待)。
#include <bitset>
#include <limits>
#include <iterator>
#include <iostream>
#include <x86intrin.h>
static std::bitset<size_t(std::numeric_limits<uint32_t>::max()) + 1> hashes;
static void findSeed()
{
uint8_t c[7];
const auto findCollision = [&] (uint32_t seed)
{
std::cout << "seed = " << seed << std::endl;
hashes.reset();
for (c[0] = 'a'; c[0] <= 'z'; ++c[0]) {
uint32_t hash0 = _mm_crc32_u8(~seed, c[0]);
for (c[1] = 'a'; c[1] <= 'z'; ++c[1]) {
uint32_t hash1 = _mm_crc32_u8(hash0, c[1]);
for (c[2] = 'a'; c[2] <= …Run Code Online (Sandbox Code Playgroud)