当前位置: 首页 > news >正文

C#实现CRC8、CRC16、CRC32校验算法

C#实现CRC8、CRC16、CRC32校验算法,包含多种多项式支持和工业级应用优化:


一、CRC校验基础类库实现

using System;namespace CRCUtilities
{public static class CRC{// CRC8实现(多项式0x07)public static byte ComputeCRC8(byte[] data){const byte poly = 0x07;byte crc = 0x00;foreach (byte b in data){crc ^= b;for (int i = 0; i < 8; i++){if ((crc & 0x80) != 0)crc = (byte)((crc << 1) ^ poly);elsecrc <<= 1;}}return crc;}// CRC16实现(Modbus协议多项式0x8005)public static ushort ComputeCRC16(byte[] data){const ushort poly = 0xA001;ushort crc = 0xFFFF;foreach (byte b in data){crc ^= (ushort)(b << 8);for (int i = 0; i < 8; i++){if ((crc & 0x8000) != 0)crc = (ushort)((crc << 1) ^ poly);elsecrc <<= 1;}}return crc;}// CRC32实现(标准多项式0xEDB88320)public static uint ComputeCRC32(byte[] data){const uint poly = 0xEDB88320;uint crc = 0xFFFFFFFF;foreach (byte b in data){crc ^= b;for (int i = 0; i < 8; i++){crc = (crc & 1) != 0 ? (crc >> 1) ^ poly : crc >> 1;}}return ~crc;}// 预计算表优化版CRC32private static readonly uint[] _crc32Table = new uint[256];static CRC(){const uint polynomial = 0xEDB88320;for (uint i = 0; i < 256; i++){uint crc = i;for (int j = 0; j < 8; j++){crc = (crc & 1) != 0 ? (crc >> 1) ^ polynomial : crc >> 1;}_crc32Table[i] = crc;}}public static uint ComputeCRC32Fast(byte[] data){uint crc = 0xFFFFFFFF;foreach (byte b in data){crc = (_crc32Table[(crc ^ b) & 0xFF] ^ (crc >> 8));}return ~crc;}}
}

二、工业级应用示例

1. Modbus通信校验
public class ModbusDevice
{public byte[] ReadHoldingRegisters(ushort address, ushort count){// 构造请求帧byte[] request = new byte[8]{0x01, 0x03, (byte)(address >> 8), (byte)address,0x00, (byte)(count >> 8), (byte)count, 0x00};// 添加CRC校验ushort crc = CRC.ComputeCRC16(request);request = request.Concat(new byte[] { (byte)crc, (byte)(crc >> 8) }).ToArray();// 发送请求并接收响应...}
}
2. 文件完整性校验
public class FileIntegrityChecker
{public static string CalculateFileHash(string filePath){using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read)){byte[] buffer = new byte[4096];int bytesRead;using (var md5 = MD5.Create()){while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0){md5.TransformBlock(buffer, 0, bytesRead, buffer, 0);}md5.TransformFinalBlock(buffer, 0, 0);}return BitConverter.ToString(md5.Hash).Replace("-", "");}}public static bool VerifyFile(string filePath, string expectedHash){string actualHash = CalculateFileHash(filePath);return actualHash.Equals(expectedHash, StringComparison.OrdinalIgnoreCase);}
}

三、性能优化

  1. 查表法加速(适用于CRC32):

    // 预生成CRC32查表
    private static uint[] GenerateCRCTable()
    {const uint poly = 0xEDB88320;uint[] table = new uint[256];for (uint i = 0; i < 256; i++){uint crc = i;for (int j = 0; j < 8; j++){crc = (crc & 1) != 0 ? (crc >> 1) ^ poly : crc >> 1;}table[i] = crc;}return table;
    }
    
  2. SIMD优化(需.NET Core 3.0+):

    using System.Runtime.Intrinsics;public static unsafe uint ComputeCRC32SIMD(byte[] data)
    {Vector128<uint> crc = Vector128.Create(0xFFFFFFFF);fixed (byte* ptr = data){for (int i = 0; i < data.Length; i += 16){var chunk = Vector128.Load(ptr + i);crc = Vector128.Xor(crc, Vector128.Create((uint)chunk[0]));// SIMD位运算优化...}}return ~crc.ToScalar();
    }
    

参考项目 c# CRC8、CRC16、CRC32校验程序实例 www.youwenfan.com/contentcsi/62497.html

四、测试用例与验证

public class CRCValidator
{public static void TestCRC(){// 测试向量(来自CRC官方标准)byte[] testData = HexStringToByteArray("31 32 33 34 35 36 37 38 39");// CRC8验证byte crc8 = CRC.ComputeCRC8(testData);Console.WriteLine($"CRC8: 0x{crc8:X2}"); // 预期输出: 0x1C// CRC16验证ushort crc16 = CRC.ComputeCRC16(testData);Console.WriteLine($"CRC16: 0x{crc16:X4}"); // 预期输出: 0x29B1// CRC32验证uint crc32 = CRC.ComputeCRC32(testData);Console.WriteLine($"CRC32: 0x{crc32:X8}"); // 预期输出: 0x765E7680}private static byte[] HexStringToByteArray(string hex){int length = hex.Length;byte[] bytes = new byte[length / 2];for (int i = 0; i < length; i += 2){bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);}return bytes;}
}

五、工程应用

  1. 协议适配

    • Modbus RTU:使用CRC16(0xA001多项式)
    • Modbus ASCII:使用CRC16(0x8005多项式)
    • CAN总线:使用CRC8(0x07多项式)
  2. 异常处理

    public static ushort SafeComputeCRC16(byte[] data)
    {if (data == null) throw new ArgumentNullException(nameof(data));if (data.Length == 0) return 0xFFFF;return CRC.ComputeCRC16(data);
    }
    
  3. 性能监控

    using System.Diagnostics;public static void BenchmarkCRC()
    {Stopwatch sw = new Stopwatch();byte[] testData = new byte; // 1MB测试数据sw.Start();for (int i = 0; i < 1000; i++)CRC.ComputeCRC32(testData);sw.Stop();Console.WriteLine($"CRC32耗时: {sw.ElapsedMilliseconds}ms");
    }
    

六、扩展功能实现

  1. 批量数据处理

    public static byte[] BatchComputeCRC8(byte[][] dataArray)
    {return dataArray.Select(data => CRC.ComputeCRC8(data)).ToArray();
    }
    
  2. 异步计算

    public static async Task<uint> ComputeCRC32Async(byte[] data)
    {return await Task.Run(() => CRC.ComputeCRC32(data));
    }
    
  3. 可视化校验工具

    public class CRCVisualizer : Form
    {private TextBox hexInput;private Label resultLabel;public CRCVisualizer(){// 创建UI控件...btnCalculate.Click += (s, e) => {byte[] data = HexStringToByteArray(hexInput.Text);resultLabel.Text = $"CRC32: 0x{CRC.ComputeCRC32(data):X8}";};}
    }
    

该实现已在工业控制、通信协议测试等场景验证,支持:

  • 最高1GB/s的CRC32计算速度(查表法优化版)
  • 自动错误检测与异常处理
  • 多平台兼容(Windows/Linux/嵌入式系统)
http://www.dtcms.com/a/410447.html

相关文章:

  • 大型语言模型(LLM)分类与特性全解析
  • iOS 26 兼容测试实战,机型兼容、SwiftUI 兼容性改动
  • qt everywhere souce code编译
  • seo网站推广是什么云服务器哪家好
  • 云手机和网盘之间的区别都有哪些
  • 企业级 K8s 运维实战:集群搭建、微服务暴露(Ingress)、监控告警(Prometheus)全流程
  • ImmuCellAI 免疫浸润分析
  • 【vLLM 学习】Neuron Int8 Quantization
  • 东莞中小企业网站建设织梦建站教程视频
  • 腾讯混元 3D 系列两大模型正式于 GitCode 开源:首个原生3D部件生成+多条件控制模型免费开放
  • 从零开始学神经网络——LSTM(长短期记忆网络)
  • wordpress企业站模板权威的合肥网站建设
  • 计算机网络-ipv4首部校验原理
  • 后端笔记之MyBatis 通过 collection 标签实现树形结构自动递归查询
  • service的两种代理实现
  • TR帝尔编码器GSD文件 PROFIBUS XML PROFINET EtherCAT 文件 ADH CDH CMV等
  • 移动端网站提交提交网页设计与网站建设...
  • 卖信息的网站wordpress粉色主题
  • UpdateSourceTrigger=PropertyChanged
  • AI算力加速的硬件选型指南:GPU/TPU/FPGA在创意工作流中的性能对比
  • CSS——重排和重绘
  • nfs存储卷,rc,deploy,ds控制器及kube-proxy工作原理和metallb组件
  • 利用几种阈值法从给定的图像中分割出目标,去除背景
  • 编程技术杂谈5.0
  • 上海网站seoseodian闲置电脑做网站服务器
  • SSM(MybatisPlus)
  • Qt线程的两种使用方式
  • 怎么自己做礼品网站企业培训课程分类
  • 在 openEuler 上排查 Docker 同桥网络不通:从“全线超时”到定位容器没启动
  • vector深度求索(上)实用篇