您好,
会员登录 快速注册
退出 ( 条未读消息)
关于本站 意见反馈 首页

公告:小宅博客网可以开发票了,需要发票的,去群里找群主哈!!
全部文章分类
  • 人工智能 >

  • 编程语言 >

  • WPF系列 >

  • ASP.NET系列 >

  • Linux >

  • 数据库 >

  • 嵌入式 >

  • WEB技术 >

  • PLC系列 >

  • 微服务与框架 >

  • 小宅DIY >

  • 学习资料 >

OpenCv基础 ANN车牌识别 yolov5车牌识别 指针式仪表识别 ROS系列 YOLO Halcon Detectron2 昇腾AI ChatGPT在线体验 英伟达JETSON ChatGLM ChatTTS FunASR 地平线 ByteTrack 魔搭社区 LangChain
C C# C++ Python Java Go
WPF
ASP.NET小功能 GPS定位系统-MVC GPS定位系统-VUE ASP.NET WebRTC
Linux Linux内核 Shell MakeFile
MySql SqlServer Oracle
STM8 STM32 51单片机
VUE入门 HTML JavaScript CSS layui镜像网站 ElementUi中文官网 element-plus 图标
三菱 欧姆龙 西门子 施耐德 松下 台达
IOTSharp IOTGateway ABP FRAMEWORK Docker
亚克力音响 编程仙途:智驭万法
面试题与技巧 Python入门技能树 微软C#教程
首页 编程之美 工具下载 全国就业 流量地图 文心一言
C#
内容简介 1、C#环境下使用EF操作MySql 2、实现串口通信 3、TCP通信 4、读写SqlServer数据库 5、读写ini配置文件 6、实现远程升级 7、调用打印机 8、AES数据加密与解密 AES加解密(RijndaelManaged版) 9、FTP的上传和下载 10、封装dll到exe 11、重写Button控件 12、重写Labeld控件 13、重写DataGridView控件 14、重写TabControl控件 15、重写ProgressBar控件 16、加载状态弹窗设计 17、模拟按键精灵 18、24种数据校验算法 19、C#如何调用opencv 20、地图上显示GPS坐标 21、使用EPPlus导出/导入xlsx格式的Excel报表和曲线图 22、exe程序生成安装包 23、DataGridView控件列控制 24、json解析 25、自定义委托事件 26、xml文件的生成与读取 27、DataGridView实现翻页效果 28、如何实现CAD dxf文件的读取 29、StopWatch的使用 30、Async与Await 同步与异步操作 31、关闭登录窗体打开主窗体的方法 32、SQLite数据库的操作 33、MD5数据加密 34、DES数据加密与解密 35、获取本地IP地址 36、打开与保存文件 37、静态图表显示(曲线图) 38、动态图表显示(曲线图) 39、根据公网IP获取地址信息 40、List转DataTable 41、C#下实现Ping操作 42、父窗体中嵌入子窗体 43、获取MySql数据库列表 44、WebSocket通讯 45、Mqtt客户端与服务端通讯 46、使用QRCode生成二维码 47、AForge调用摄像头 48、Emgu.CV调用摄像头 49、获取以太网网卡IP 50、DataGridView全选与定位 51、如何获取系统中所有程序的句柄 遍历指定目录下的所有文件 ​NPOI Excel报表的导入与导出 常用正则表达式字符串格式判断 Task和Thread的启停操作 C# 一些常用小功能1 C# 一些常用小功能2 如何使用C#来发送QQ邮件 Aspose.Slides文档格式转换 C# GDI+ 画心形 跳动动画 使用Remoting实现RPC RabbitMQ.消息发布与订阅 .Net Core 微信/支付宝 官方Demo C# OCR图片文字识别 Quartz 计划任务 文字语音播报 winform基于百度地图的电子围栏、路径规划、小车导航实现 Visual Studio C盘数据迁移,解决C盘空间不足的问题 Kafka通讯(Kafka-Net版) Kafka通讯(Confluent.Kafka版) 获取变量Description描述 C# 将PDF文档转换为Word文档 C# MVC 多图片上传预览
17、模拟按键精灵
19、C#如何调用opencv
激萌の小宅 小宅博客网 C#

文章作者:激萌の小宅

促销:¥0

价格:¥0

配送方式: 购买后立即生效(如购买异常,请联系站长)
付款之后一定要等待自动跳转结束,否则购买可能会失败
  • 0 天

    有效期

  • 0

    总销量

  • 0

    累计评价

24种数据校验算法 - (第十八讲)


视频讲解如下:


CSDN源码下载

https://download.csdn.net/download/gs1069405343/19640990

网盘下载,提取码:hua7

https://pan.baidu.com/s/1thNmtm7KDGgoaDDLcm6xXw


这里给大家讲解一下如何实现数据通信中常用的24种数据校验算法


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApp
{
    public class crcs
    {
        /// WIDTH:宽度,即CRC比特数。
        /// POLY:生成项的简写,以16进制表示。例如:CRC-32即是0x04C11DB7,忽略了最高位的"1",即完整的生成项是0x104C11DB7。
        /// INIT:这是算法开始时寄存器(crc)的初始化预置值,十六进制表示。
        /// REFIN:待测数据的每个字节是否按位反转,True或False。
        /// REFOUT:在计算后之后,异或输出之前,整个数据是否按位反转,True或False。
        /// XOROUT:计算结果与此参数异或后得到最终的CRC值。

        ///******************************************************************************
        /// * Name:    CRC-4/ITU           x4+x+1
        /// * Poly:    0x03
        /// * Init:    0x00
        /// * Refin:   True
        /// * Refout:  True
        /// * Xorout:  0x00
        /// * Note:
        /// *****************************************************************************/
        public byte[] CRC4_ITU(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            byte crc = 0;
            for (int i = 0; i < buffer.Length; i++)
            {
                crc ^= buffer[i];
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 1) > 0)
                        crc = (byte)((crc >> 1) ^ 0x0C);
                    else
                        crc = (byte)(crc >> 1);
                }
            }
            return new byte[] { crc };
        }

        /// **********************************************************************
        /// Name: CRC-5/EPC    x5+x3+1
        /// Poly: 0x09
        /// Init: 0x09
        /// Refin: false
        /// Refout: false
        /// Xorout: 0x00
        ///*************************************************************************
        public byte[] CRC5_EPC(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            int length = buffer.Length;
            byte crc = 0x48;// Initial value: 0x48 = 0x09<<(8-5)
            for (int i = 0; i < length; i++)
            {
                crc ^= buffer[i];
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 0x80) > 0)
                        crc = (byte)((crc << 1) ^ 0x48);// 0x48 = 0x09<<(8-5)
                    else
                        crc = (byte)(crc << 1);
                }
            }
            return new byte[] { (byte)(crc >> 3) };
        }

        /// **********************************************************************
        /// Name: CRC-5/ITU    x5+x4+x2+1
        /// Poly: 0x15
        /// Init: 0x00
        /// Refin: true
        /// Refout: true
        /// Xorout: 0x00
        ///*************************************************************************
        public byte[] CRC5_ITU(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            int length = buffer.Length;
            byte crc = 0;// Initial value
            for (int i = 0; i < length; i++)
            {
                crc ^= buffer[i];
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 1) > 0)
                        crc = (byte)((crc >> 1) ^ 0x15);// 0x15 = (reverse 0x15)>>(8-5)
                    else
                        crc = (byte)(crc >> 1);
                }
            }
            return new byte[] { crc };
        }

        /// **********************************************************************
        /// Name: CRC-5/USB    x5+x2+1
        /// Poly: 0x05
        /// Init: 0x1F
        /// Refin: true
        /// Refout: true
        /// Xorout: 0x1F
        ///*************************************************************************
        public byte[] CRC5_USB(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            int length = buffer.Length;
            byte crc = 0x1F;// Initial value
            for (int i = 0; i < length; i++)
            {
                crc ^= buffer[i];
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 1) > 0)
                        crc = (byte)((crc >> 1) ^ 0x14);// 0x14 = (reverse 0x05)>>(8-5)
                    else
                        crc = (byte)(crc >> 1);
                }
            }
            return new byte[] { (byte)(crc ^ 0x1F) };
        }

        /// **********************************************************************
        /// Name: CRC-6/ITU    x6+x+1
        /// Poly: 0x03
        /// Init: 0x00
        /// Refin: true
        /// Refout: true
        /// Xorout: 0x00
        ///*************************************************************************
        public byte[] CRC6_ITU(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            int length = buffer.Length;
            byte crc = 0;// Initial value
            for (int i = 0; i < length; i++)
            {
                crc ^= buffer[i];
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 1) > 0)
                        crc = (byte)((crc >> 1) ^ 0x30);// 0x30 = (reverse 0x03)>>(8-6)
                    else
                        crc = (byte)(crc >> 1);
                }
            }
            return new byte[] { crc };
        }

        /// **********************************************************************
        /// Name: CRC-7/MMC    x7+x3+1
        /// Poly: 0x09
        /// Init: 0x00
        /// Refin: false
        /// Refout: false
        /// Xorout: 0x00
        ///*************************************************************************
        public byte[] CRC7_MMC(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            int length = buffer.Length;
            byte crc = 0;// Initial value
            for (int i = 0; i < length; i++)
            {
                crc ^= buffer[i];
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 0x80) > 0)
                        crc = (byte)((crc << 1) ^ 0x12);// 0x12 = 0x09<<(8-7)
                    else
                        crc = (byte)(crc << 1);
                }
            }
            return new byte[] { (byte)(crc >> 1) };
        }

        /// **********************************************************************
        /// Name: CRC8    x8+x2+x+1
        /// Poly: 0x07
        /// Init: 0x00
        /// Refin: false
        /// Refout: false
        /// Xorout: 0x00
        ///*************************************************************************
        public byte[] CRC8(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            int length = buffer.Length;
            byte crc = 0;// Initial value
            for (int i = 0; i < length; i++)
            {
                crc ^= buffer[i];
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 0x80) > 0)
                        crc = (byte)((crc << 1) ^ 0x07);
                    else
                        crc = (byte)(crc << 1);
                }
            }
            return new byte[] { crc };
        }

        /// **********************************************************************
        /// Name: CRC-8/ITU    x8+x2+x+1
        /// Poly: 0x07
        /// Init: 0x00
        /// Refin: false
        /// Refout: false
        /// Xorout: 0x55
        ///*************************************************************************
        public byte[] CRC8_ITU(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            int length = buffer.Length;
            byte crc = 0;// Initial value
            for (int i = 0; i < length; i++)
            {
                crc ^= buffer[i];
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 0x80) > 0)
                        crc = (byte)((crc << 1) ^ 0x07);
                    else
                        crc = (byte)(crc << 1);
                }
            }
            return new byte[] { (byte)(crc ^ 0x55) };
        }

        /// **********************************************************************
        /// Name: CRC-8/ROHC    x8+x2+x+1
        /// Poly: 0x07
        /// Init: 0xFF
        /// Refin: true
        /// Refout: true
        /// Xorout: 0x00
        ///*************************************************************************
        public byte[] CRC8_ROHC(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            int length = buffer.Length;
            byte crc = 0xFF;// Initial value
            for (int i = 0; i < length; i++)
            {
                crc ^= buffer[i];
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 1) > 0)
                        crc = (byte)((crc >> 1) ^ 0xE0);// 0xE0 = reverse 0x07
                    else
                        crc = (byte)(crc >> 1);
                }
            }
            return new byte[] { crc };
        }

        /// **********************************************************************
        /// Name: CRC-8/MAXIM    x8+x5+x4+1
        /// Poly: 0x31
        /// Init: 0x00
        /// Refin: true
        /// Refout: true
        /// Xorout: 0x00
        ///*************************************************************************
        public byte[] CRC8_MAXIM(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            int length = buffer.Length;
            byte crc = 0;// Initial value
            for (int i = 0; i < length; i++)
            {
                crc ^= buffer[i];
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 1) > 0)
                        crc = (byte)((crc >> 1) ^ 0x8C);// 0x8C = reverse 0x31
                    else
                        crc = (byte)(crc >> 1);
                }
            }
            return new byte[] { crc };
        }

        /// **********************************************************************
        /// Name: CRC-16/IBM    x16+x15+x2+1
        /// Poly: 0x8005
        /// Init: 0x0000
        /// Refin: true
        /// Refout: true
        /// Xorout: 0x0000
        ///*************************************************************************
        public byte[] CRC16_IBM(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            int length = buffer.Length;
            ushort crc = 0;// Initial value
            for (int i = 0; i < length; i++)
            {
                crc ^= buffer[i];
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 1) > 0)
                        crc = (ushort)((crc >> 1) ^ 0xA001);// 0xA001 = reverse 0x8005
                    else
                        crc = (ushort)(crc >> 1);
                }
            }
            byte[] ret = BitConverter.GetBytes(crc);
            Array.Reverse(ret);
            return ret;
        }

        /// **********************************************************************
        /// Name: CRC-16/MAXIM    x16+x15+x2+1
        /// Poly: 0x8005
        /// Init: 0x0000
        /// Refin: true
        /// Refout: true
        /// Xorout: 0xFFFF
        ///*************************************************************************
        public byte[] CRC16_MAXIM(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            int length = buffer.Length;
            ushort crc = 0;// Initial value
            for (int i = 0; i < length; i++)
            {
                crc ^= buffer[i];
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 1) > 0)
                        crc = (ushort)((crc >> 1) ^ 0xA001);// 0xA001 = reverse 0x8005
                    else
                        crc = (ushort)(crc >> 1);
                }
            }
            byte[] ret = BitConverter.GetBytes((ushort)~crc);
            Array.Reverse(ret);
            return ret;
        }

        /// **********************************************************************
        /// Name: CRC-16/USB    x16+x15+x2+1
        /// Poly: 0x8005
        /// Init: 0xFFFF
        /// Refin: true
        /// Refout: true
        /// Xorout: 0xFFFF
        ///*************************************************************************
        public byte[] CRC16_USB(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            int length = buffer.Length;
            ushort crc = 0xFFFF;// Initial value
            for (int i = 0; i < length; i++)
            {
                crc ^= buffer[i];
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 1) > 0)
                        crc = (ushort)((crc >> 1) ^ 0xA001);// 0xA001 = reverse 0x8005
                    else
                        crc = (ushort)(crc >> 1);
                }
            }
            byte[] ret = BitConverter.GetBytes((ushort)~crc);
            Array.Reverse(ret);
            return ret;
        }

        /// **********************************************************************
        /// Name: CRC-16/MODBUS    x16+x15+x2+1
        /// Poly: 0x8005
        /// Init: 0xFFFF
        /// Refin: true
        /// Refout: true
        /// Xorout: 0x0000
        ///*************************************************************************
        public byte[] CRC16_MODBUS(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            int length = buffer.Length;
            ushort crc = 0xFFFF;// Initial value
            for (int i = 0; i < length; i++)
            {
                crc ^= buffer[i];
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 1) > 0)
                        crc = (ushort)((crc >> 1) ^ 0xA001);// 0xA001 = reverse 0x8005
                    else
                        crc = (ushort)(crc >> 1);
                }
            }
            byte[] ret = BitConverter.GetBytes(crc);
            Array.Reverse(ret);
            return ret;
        }

        /// **********************************************************************
        /// Name: CRC-16/CCITT    x16+x12+x5+1
        /// Poly: 0x1021
        /// Init: 0x0000
        /// Refin: true
        /// Refout: true
        /// Xorout: 0x0000
        ///*************************************************************************
        public byte[] CRC16_CCITT(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            int length = buffer.Length;
            ushort crc = 0;// Initial value
            for (int i = 0; i < length; i++)
            {
                crc ^= buffer[i];
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 1) > 0)
                        crc = (ushort)((crc >> 1) ^ 0x8408);// 0x8408 = reverse 0x1021
                    else
                        crc = (ushort)(crc >> 1);
                }
            }
            byte[] ret = BitConverter.GetBytes(crc);
            Array.Reverse(ret);
            return ret;
        }

        /// **********************************************************************
        /// Name: CRC-16/CCITT FALSE    x16+x12+x5+1
        /// Poly: 0x1021
        /// Init: 0xFFFF
        /// Refin: false
        /// Refout: false
        /// Xorout: 0x0000
        ///*************************************************************************
        public byte[] CRC16_CCITT_FALSE(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            int length = buffer.Length;
            ushort crc = 0xFFFF;// Initial value
            for (int i = 0; i < length; i++)
            {
                crc ^= (ushort)(buffer[i] << 8);
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 0x8000) > 0)
                        crc = (ushort)((crc << 1) ^ 0x1021);
                    else
                        crc = (ushort)(crc << 1);
                }
            }
            byte[] ret = BitConverter.GetBytes(crc);
            Array.Reverse(ret);
            return ret;
        }

        /// **********************************************************************
        /// Name: CRC-16/X25    x16+x12+x5+1
        /// Poly: 0x1021
        /// Init: 0xFFFF
        /// Refin: true
        /// Refout: true
        /// Xorout: 0xFFFF
        ///*************************************************************************
        public byte[] CRC16_X25(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            int length = buffer.Length;
            ushort crc = 0xFFFF;// Initial value
            for (int i = 0; i < length; i++)
            {
                crc ^= buffer[i];
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 1) > 0)
                        crc = (ushort)((crc >> 1) ^ 0x8408);// 0x8408 = reverse 0x1021
                    else
                        crc = (ushort)(crc >> 1);
                }
            }
            byte[] ret = BitConverter.GetBytes((ushort)~crc);
            Array.Reverse(ret);
            return ret;
        }

        /// **********************************************************************
        /// Name: CRC-16/XMODEM    x16+x12+x5+1
        /// Poly: 0x1021
        /// Init: 0x0000
        /// Refin: false
        /// Refout: false
        /// Xorout: 0x0000
        ///*************************************************************************
        public byte[] CRC16_XMODEM(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            int length = buffer.Length;
            ushort crc = 0;// Initial value
            for (int i = 0; i < length; i++)
            {
                crc ^= (ushort)(buffer[i] << 8);
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 0x8000) > 0)
                        crc = (ushort)((crc << 1) ^ 0x1021);
                    else
                        crc = (ushort)(crc << 1);
                }
            }
            byte[] ret = BitConverter.GetBytes(crc);
            Array.Reverse(ret);
            return ret;
        }

        /// **********************************************************************
        /// Name: CRC-16/DNP    x16+x13+x12+x11+x10+x8+x6+x5+x2+1
        /// Poly: 0x3D65
        /// Init: 0x0000
        /// Refin: true
        /// Refout: true
        /// Xorout: 0xFFFF
        ///*************************************************************************
        public byte[] CRC16_DNP(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            int length = buffer.Length;
            ushort crc = 0;// Initial value
            for (int i = 0; i < length; i++)
            {
                crc ^= buffer[i];
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 1) > 0)
                        crc = (ushort)((crc >> 1) ^ 0xA6BC);// 0xA6BC = reverse 0x3D65
                    else
                        crc = (ushort)(crc >> 1);
                }
            }
            byte[] ret = BitConverter.GetBytes((ushort)~crc);
            Array.Reverse(ret);
            return ret;
        }

        /// **********************************************************************
        /// Name: CRC32    x32+x26+x23+x22+x16+x12+x11+x10+x8+x7+x5+x4+x2+x+1
        /// Poly: 0x04C11DB7
        /// Init: 0xFFFFFFFF
        /// Refin: true
        /// Refout: true
        /// Xorout: 0xFFFFFFFF
        ///*************************************************************************
        public byte[] CRC32(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            int length = buffer.Length;
            uint crc = 0xFFFFFFFF;// Initial value
            for (int i = 0; i < length; i++)
            {
                crc ^= buffer[i];
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 1) > 0)
                        crc = (crc >> 1) ^ 0xEDB88320;// 0xEDB88320= reverse 0x04C11DB7
                    else
                        crc = crc >> 1;
                }
            }
            byte[] ret = BitConverter.GetBytes(~crc);
            Array.Reverse(ret);
            return ret;
        }

        /// **********************************************************************
        /// Name: CRC32/MPEG-2    x32+x26+x23+x22+x16+x12+x11+x10+x8+x7+x5+x4+x2+x+1
        /// Poly: 0x04C11DB7
        /// Init: 0xFFFFFFFF
        /// Refin: false
        /// Refout: false
        /// Xorout: 0x00000000
        ///*************************************************************************
        public byte[] CRC32_MPEG2(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            int length = buffer.Length;
            uint crc = 0xFFFFFFFF;// Initial value
            for (int i = 0; i < length; i++)
            {
                crc ^= (uint)(buffer[i] << 24);
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 0x80000000) > 0)
                        crc = (crc << 1) ^ 0x04C11DB7;
                    else
                        crc = crc << 1;
                }
            }
            byte[] ret = BitConverter.GetBytes(crc);
            Array.Reverse(ret);
            return ret;
        }

        // 纵向冗余校验(Longitudinal Redundancy Check,简称:LRC)是通信中常用的一种校验形式,也称LRC校验或纵向校验。 
        // 它是一种从纵向通道上的特定比特串产生校验比特的错误检测方法。在行列格式中(如磁带),LRC经常是与VRC一起使用,
        // 这样就会为每个字符校验码。在工业领域Modbus协议Ascii模式采用该算法。 LRC计算校验码,具体算法如下:
        /// 1、对需要校验的数据(2n个字符)两两组成一个16进制的数值求和。
        /// 2、将求和结果与256求模。
        /// 3、用256减去所得模值得到校验结果(另一种方法:将模值按位取反然后加1)。
        public byte[] LRC(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            byte lrc = 0;
            for (int i = 0; i < buffer.Length; i++)
            {
                lrc += buffer[i];
            }
            lrc = (byte)((lrc ^ 0xFF) + 1);
            return new byte[] { lrc };
        }

        // BCC(Block Check Character/信息组校验码),因校验码是将所有数据异或得出,故俗称异或校验。
        // 具体算法是:将每一个字节的数据(一般是两个16进制的字符)进行异或后即得到校验码。
        public byte[] BCC(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            byte bcc = 0;// Initial value
            for (int i = 0; i < buffer.Length; i++)
            {
                bcc ^= buffer[i];
            }
            return new byte[] { bcc };
        }

        /// <summary>
        /// 和校验
        /// </summary>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public byte[] ADD(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            int value = 0;
            for (int i = 0; i < buffer.Length; i++)
            {
                value += buffer[i];
            }
            byte bcc = (byte)(value & 0x00FF);
            return new byte[] { bcc };
        }
    }
}


private void button1_Click(object sender, EventArgs e)
{
    byte[] Data = Encoding.UTF8.GetBytes(textBox1.Text);
    if (Data == null) return;
    byte[] a = null;

    if (comboBox1.SelectedIndex == 0) a = new crcs().CRC4_ITU(Data);
    if (comboBox1.SelectedIndex == 1) a = new crcs().CRC5_EPC(Data);
    if (comboBox1.SelectedIndex == 2) a = new crcs().CRC5_ITU(Data);
    if (comboBox1.SelectedIndex == 3) a = new crcs().CRC5_USB(Data);
    if (comboBox1.SelectedIndex == 4) a = new crcs().CRC6_ITU(Data);
    if (comboBox1.SelectedIndex == 5) a = new crcs().CRC7_MMC(Data);
    if (comboBox1.SelectedIndex == 6) a = new crcs().CRC8(Data);
    if (comboBox1.SelectedIndex == 7) a = new crcs().CRC8_ITU(Data);
    if (comboBox1.SelectedIndex == 8) a = new crcs().CRC8_ROHC(Data);
    if (comboBox1.SelectedIndex == 9) a = new crcs().CRC8_MAXIM(Data);
    if (comboBox1.SelectedIndex == 10) a = new crcs().CRC16_IBM(Data);
    if (comboBox1.SelectedIndex == 11) a = new crcs().CRC16_MAXIM(Data);
    if (comboBox1.SelectedIndex == 12) a = new crcs().CRC16_USB(Data);
    if (comboBox1.SelectedIndex == 13) a = new crcs().CRC16_MODBUS(Data);
    if (comboBox1.SelectedIndex == 14) a = new crcs().CRC16_CCITT(Data);
    if (comboBox1.SelectedIndex == 15) a = new crcs().CRC16_CCITT_FALSE(Data);
    if (comboBox1.SelectedIndex == 16) a = new crcs().CRC16_X25(Data);
    if (comboBox1.SelectedIndex == 17) a = new crcs().CRC16_XMODEM(Data);
    if (comboBox1.SelectedIndex == 18) a = new crcs().CRC16_DNP(Data);
    if (comboBox1.SelectedIndex == 19) a = new crcs().CRC32(Data);
    if (comboBox1.SelectedIndex == 20) a = new crcs().CRC32_MPEG2(Data);
    if (comboBox1.SelectedIndex == 21) a = new crcs().LRC(Data);
    if (comboBox1.SelectedIndex == 22) a = new crcs().BCC(Data);
    if (comboBox1.SelectedIndex == 23) a = new crcs().ADD(Data);

    if (a != null) textBox2.Text = BitConverter.ToString(a).Replace("-", " ");
}
17、模拟按键精灵
19、C#如何调用opencv

友情链接: CSDN激萌の小宅 95知识库 自考题库 罗分明个人网络博客 精益编程leanboot

小宅博客  www.bilibili996.com All Rights Reserved. 备案号: 闽ICP备2024034575号

网站经营许可证  福建省福州市 Copyright©2021-2025 版权所有

小宅博客
首页 智能家居 地图定位
公告:小宅博客网可以开发票了,需要发票的,去群里找群主哈!!

文章作者:激萌の小宅

促销:¥0

价格:¥0

配送方式: 购买后立即生效(如购买异常,请联系站长)
付款之后一定要等待自动跳转结束,否则购买可能会失败
  • 0 天

    有效期

  • 0

    总销量

  • 0

    累计评价

24种数据校验算法 - (第十八讲)


视频讲解如下:


CSDN源码下载

https://download.csdn.net/download/gs1069405343/19640990

网盘下载,提取码:hua7

https://pan.baidu.com/s/1thNmtm7KDGgoaDDLcm6xXw


这里给大家讲解一下如何实现数据通信中常用的24种数据校验算法


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApp
{
    public class crcs
    {
        /// WIDTH:宽度,即CRC比特数。
        /// POLY:生成项的简写,以16进制表示。例如:CRC-32即是0x04C11DB7,忽略了最高位的"1",即完整的生成项是0x104C11DB7。
        /// INIT:这是算法开始时寄存器(crc)的初始化预置值,十六进制表示。
        /// REFIN:待测数据的每个字节是否按位反转,True或False。
        /// REFOUT:在计算后之后,异或输出之前,整个数据是否按位反转,True或False。
        /// XOROUT:计算结果与此参数异或后得到最终的CRC值。

        ///******************************************************************************
        /// * Name:    CRC-4/ITU           x4+x+1
        /// * Poly:    0x03
        /// * Init:    0x00
        /// * Refin:   True
        /// * Refout:  True
        /// * Xorout:  0x00
        /// * Note:
        /// *****************************************************************************/
        public byte[] CRC4_ITU(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            byte crc = 0;
            for (int i = 0; i < buffer.Length; i++)
            {
                crc ^= buffer[i];
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 1) > 0)
                        crc = (byte)((crc >> 1) ^ 0x0C);
                    else
                        crc = (byte)(crc >> 1);
                }
            }
            return new byte[] { crc };
        }

        /// **********************************************************************
        /// Name: CRC-5/EPC    x5+x3+1
        /// Poly: 0x09
        /// Init: 0x09
        /// Refin: false
        /// Refout: false
        /// Xorout: 0x00
        ///*************************************************************************
        public byte[] CRC5_EPC(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            int length = buffer.Length;
            byte crc = 0x48;// Initial value: 0x48 = 0x09<<(8-5)
            for (int i = 0; i < length; i++)
            {
                crc ^= buffer[i];
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 0x80) > 0)
                        crc = (byte)((crc << 1) ^ 0x48);// 0x48 = 0x09<<(8-5)
                    else
                        crc = (byte)(crc << 1);
                }
            }
            return new byte[] { (byte)(crc >> 3) };
        }

        /// **********************************************************************
        /// Name: CRC-5/ITU    x5+x4+x2+1
        /// Poly: 0x15
        /// Init: 0x00
        /// Refin: true
        /// Refout: true
        /// Xorout: 0x00
        ///*************************************************************************
        public byte[] CRC5_ITU(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            int length = buffer.Length;
            byte crc = 0;// Initial value
            for (int i = 0; i < length; i++)
            {
                crc ^= buffer[i];
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 1) > 0)
                        crc = (byte)((crc >> 1) ^ 0x15);// 0x15 = (reverse 0x15)>>(8-5)
                    else
                        crc = (byte)(crc >> 1);
                }
            }
            return new byte[] { crc };
        }

        /// **********************************************************************
        /// Name: CRC-5/USB    x5+x2+1
        /// Poly: 0x05
        /// Init: 0x1F
        /// Refin: true
        /// Refout: true
        /// Xorout: 0x1F
        ///*************************************************************************
        public byte[] CRC5_USB(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            int length = buffer.Length;
            byte crc = 0x1F;// Initial value
            for (int i = 0; i < length; i++)
            {
                crc ^= buffer[i];
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 1) > 0)
                        crc = (byte)((crc >> 1) ^ 0x14);// 0x14 = (reverse 0x05)>>(8-5)
                    else
                        crc = (byte)(crc >> 1);
                }
            }
            return new byte[] { (byte)(crc ^ 0x1F) };
        }

        /// **********************************************************************
        /// Name: CRC-6/ITU    x6+x+1
        /// Poly: 0x03
        /// Init: 0x00
        /// Refin: true
        /// Refout: true
        /// Xorout: 0x00
        ///*************************************************************************
        public byte[] CRC6_ITU(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            int length = buffer.Length;
            byte crc = 0;// Initial value
            for (int i = 0; i < length; i++)
            {
                crc ^= buffer[i];
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 1) > 0)
                        crc = (byte)((crc >> 1) ^ 0x30);// 0x30 = (reverse 0x03)>>(8-6)
                    else
                        crc = (byte)(crc >> 1);
                }
            }
            return new byte[] { crc };
        }

        /// **********************************************************************
        /// Name: CRC-7/MMC    x7+x3+1
        /// Poly: 0x09
        /// Init: 0x00
        /// Refin: false
        /// Refout: false
        /// Xorout: 0x00
        ///*************************************************************************
        public byte[] CRC7_MMC(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            int length = buffer.Length;
            byte crc = 0;// Initial value
            for (int i = 0; i < length; i++)
            {
                crc ^= buffer[i];
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 0x80) > 0)
                        crc = (byte)((crc << 1) ^ 0x12);// 0x12 = 0x09<<(8-7)
                    else
                        crc = (byte)(crc << 1);
                }
            }
            return new byte[] { (byte)(crc >> 1) };
        }

        /// **********************************************************************
        /// Name: CRC8    x8+x2+x+1
        /// Poly: 0x07
        /// Init: 0x00
        /// Refin: false
        /// Refout: false
        /// Xorout: 0x00
        ///*************************************************************************
        public byte[] CRC8(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            int length = buffer.Length;
            byte crc = 0;// Initial value
            for (int i = 0; i < length; i++)
            {
                crc ^= buffer[i];
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 0x80) > 0)
                        crc = (byte)((crc << 1) ^ 0x07);
                    else
                        crc = (byte)(crc << 1);
                }
            }
            return new byte[] { crc };
        }

        /// **********************************************************************
        /// Name: CRC-8/ITU    x8+x2+x+1
        /// Poly: 0x07
        /// Init: 0x00
        /// Refin: false
        /// Refout: false
        /// Xorout: 0x55
        ///*************************************************************************
        public byte[] CRC8_ITU(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            int length = buffer.Length;
            byte crc = 0;// Initial value
            for (int i = 0; i < length; i++)
            {
                crc ^= buffer[i];
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 0x80) > 0)
                        crc = (byte)((crc << 1) ^ 0x07);
                    else
                        crc = (byte)(crc << 1);
                }
            }
            return new byte[] { (byte)(crc ^ 0x55) };
        }

        /// **********************************************************************
        /// Name: CRC-8/ROHC    x8+x2+x+1
        /// Poly: 0x07
        /// Init: 0xFF
        /// Refin: true
        /// Refout: true
        /// Xorout: 0x00
        ///*************************************************************************
        public byte[] CRC8_ROHC(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            int length = buffer.Length;
            byte crc = 0xFF;// Initial value
            for (int i = 0; i < length; i++)
            {
                crc ^= buffer[i];
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 1) > 0)
                        crc = (byte)((crc >> 1) ^ 0xE0);// 0xE0 = reverse 0x07
                    else
                        crc = (byte)(crc >> 1);
                }
            }
            return new byte[] { crc };
        }

        /// **********************************************************************
        /// Name: CRC-8/MAXIM    x8+x5+x4+1
        /// Poly: 0x31
        /// Init: 0x00
        /// Refin: true
        /// Refout: true
        /// Xorout: 0x00
        ///*************************************************************************
        public byte[] CRC8_MAXIM(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            int length = buffer.Length;
            byte crc = 0;// Initial value
            for (int i = 0; i < length; i++)
            {
                crc ^= buffer[i];
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 1) > 0)
                        crc = (byte)((crc >> 1) ^ 0x8C);// 0x8C = reverse 0x31
                    else
                        crc = (byte)(crc >> 1);
                }
            }
            return new byte[] { crc };
        }

        /// **********************************************************************
        /// Name: CRC-16/IBM    x16+x15+x2+1
        /// Poly: 0x8005
        /// Init: 0x0000
        /// Refin: true
        /// Refout: true
        /// Xorout: 0x0000
        ///*************************************************************************
        public byte[] CRC16_IBM(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            int length = buffer.Length;
            ushort crc = 0;// Initial value
            for (int i = 0; i < length; i++)
            {
                crc ^= buffer[i];
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 1) > 0)
                        crc = (ushort)((crc >> 1) ^ 0xA001);// 0xA001 = reverse 0x8005
                    else
                        crc = (ushort)(crc >> 1);
                }
            }
            byte[] ret = BitConverter.GetBytes(crc);
            Array.Reverse(ret);
            return ret;
        }

        /// **********************************************************************
        /// Name: CRC-16/MAXIM    x16+x15+x2+1
        /// Poly: 0x8005
        /// Init: 0x0000
        /// Refin: true
        /// Refout: true
        /// Xorout: 0xFFFF
        ///*************************************************************************
        public byte[] CRC16_MAXIM(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            int length = buffer.Length;
            ushort crc = 0;// Initial value
            for (int i = 0; i < length; i++)
            {
                crc ^= buffer[i];
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 1) > 0)
                        crc = (ushort)((crc >> 1) ^ 0xA001);// 0xA001 = reverse 0x8005
                    else
                        crc = (ushort)(crc >> 1);
                }
            }
            byte[] ret = BitConverter.GetBytes((ushort)~crc);
            Array.Reverse(ret);
            return ret;
        }

        /// **********************************************************************
        /// Name: CRC-16/USB    x16+x15+x2+1
        /// Poly: 0x8005
        /// Init: 0xFFFF
        /// Refin: true
        /// Refout: true
        /// Xorout: 0xFFFF
        ///*************************************************************************
        public byte[] CRC16_USB(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            int length = buffer.Length;
            ushort crc = 0xFFFF;// Initial value
            for (int i = 0; i < length; i++)
            {
                crc ^= buffer[i];
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 1) > 0)
                        crc = (ushort)((crc >> 1) ^ 0xA001);// 0xA001 = reverse 0x8005
                    else
                        crc = (ushort)(crc >> 1);
                }
            }
            byte[] ret = BitConverter.GetBytes((ushort)~crc);
            Array.Reverse(ret);
            return ret;
        }

        /// **********************************************************************
        /// Name: CRC-16/MODBUS    x16+x15+x2+1
        /// Poly: 0x8005
        /// Init: 0xFFFF
        /// Refin: true
        /// Refout: true
        /// Xorout: 0x0000
        ///*************************************************************************
        public byte[] CRC16_MODBUS(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            int length = buffer.Length;
            ushort crc = 0xFFFF;// Initial value
            for (int i = 0; i < length; i++)
            {
                crc ^= buffer[i];
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 1) > 0)
                        crc = (ushort)((crc >> 1) ^ 0xA001);// 0xA001 = reverse 0x8005
                    else
                        crc = (ushort)(crc >> 1);
                }
            }
            byte[] ret = BitConverter.GetBytes(crc);
            Array.Reverse(ret);
            return ret;
        }

        /// **********************************************************************
        /// Name: CRC-16/CCITT    x16+x12+x5+1
        /// Poly: 0x1021
        /// Init: 0x0000
        /// Refin: true
        /// Refout: true
        /// Xorout: 0x0000
        ///*************************************************************************
        public byte[] CRC16_CCITT(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            int length = buffer.Length;
            ushort crc = 0;// Initial value
            for (int i = 0; i < length; i++)
            {
                crc ^= buffer[i];
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 1) > 0)
                        crc = (ushort)((crc >> 1) ^ 0x8408);// 0x8408 = reverse 0x1021
                    else
                        crc = (ushort)(crc >> 1);
                }
            }
            byte[] ret = BitConverter.GetBytes(crc);
            Array.Reverse(ret);
            return ret;
        }

        /// **********************************************************************
        /// Name: CRC-16/CCITT FALSE    x16+x12+x5+1
        /// Poly: 0x1021
        /// Init: 0xFFFF
        /// Refin: false
        /// Refout: false
        /// Xorout: 0x0000
        ///*************************************************************************
        public byte[] CRC16_CCITT_FALSE(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            int length = buffer.Length;
            ushort crc = 0xFFFF;// Initial value
            for (int i = 0; i < length; i++)
            {
                crc ^= (ushort)(buffer[i] << 8);
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 0x8000) > 0)
                        crc = (ushort)((crc << 1) ^ 0x1021);
                    else
                        crc = (ushort)(crc << 1);
                }
            }
            byte[] ret = BitConverter.GetBytes(crc);
            Array.Reverse(ret);
            return ret;
        }

        /// **********************************************************************
        /// Name: CRC-16/X25    x16+x12+x5+1
        /// Poly: 0x1021
        /// Init: 0xFFFF
        /// Refin: true
        /// Refout: true
        /// Xorout: 0xFFFF
        ///*************************************************************************
        public byte[] CRC16_X25(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            int length = buffer.Length;
            ushort crc = 0xFFFF;// Initial value
            for (int i = 0; i < length; i++)
            {
                crc ^= buffer[i];
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 1) > 0)
                        crc = (ushort)((crc >> 1) ^ 0x8408);// 0x8408 = reverse 0x1021
                    else
                        crc = (ushort)(crc >> 1);
                }
            }
            byte[] ret = BitConverter.GetBytes((ushort)~crc);
            Array.Reverse(ret);
            return ret;
        }

        /// **********************************************************************
        /// Name: CRC-16/XMODEM    x16+x12+x5+1
        /// Poly: 0x1021
        /// Init: 0x0000
        /// Refin: false
        /// Refout: false
        /// Xorout: 0x0000
        ///*************************************************************************
        public byte[] CRC16_XMODEM(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            int length = buffer.Length;
            ushort crc = 0;// Initial value
            for (int i = 0; i < length; i++)
            {
                crc ^= (ushort)(buffer[i] << 8);
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 0x8000) > 0)
                        crc = (ushort)((crc << 1) ^ 0x1021);
                    else
                        crc = (ushort)(crc << 1);
                }
            }
            byte[] ret = BitConverter.GetBytes(crc);
            Array.Reverse(ret);
            return ret;
        }

        /// **********************************************************************
        /// Name: CRC-16/DNP    x16+x13+x12+x11+x10+x8+x6+x5+x2+1
        /// Poly: 0x3D65
        /// Init: 0x0000
        /// Refin: true
        /// Refout: true
        /// Xorout: 0xFFFF
        ///*************************************************************************
        public byte[] CRC16_DNP(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            int length = buffer.Length;
            ushort crc = 0;// Initial value
            for (int i = 0; i < length; i++)
            {
                crc ^= buffer[i];
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 1) > 0)
                        crc = (ushort)((crc >> 1) ^ 0xA6BC);// 0xA6BC = reverse 0x3D65
                    else
                        crc = (ushort)(crc >> 1);
                }
            }
            byte[] ret = BitConverter.GetBytes((ushort)~crc);
            Array.Reverse(ret);
            return ret;
        }

        /// **********************************************************************
        /// Name: CRC32    x32+x26+x23+x22+x16+x12+x11+x10+x8+x7+x5+x4+x2+x+1
        /// Poly: 0x04C11DB7
        /// Init: 0xFFFFFFFF
        /// Refin: true
        /// Refout: true
        /// Xorout: 0xFFFFFFFF
        ///*************************************************************************
        public byte[] CRC32(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            int length = buffer.Length;
            uint crc = 0xFFFFFFFF;// Initial value
            for (int i = 0; i < length; i++)
            {
                crc ^= buffer[i];
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 1) > 0)
                        crc = (crc >> 1) ^ 0xEDB88320;// 0xEDB88320= reverse 0x04C11DB7
                    else
                        crc = crc >> 1;
                }
            }
            byte[] ret = BitConverter.GetBytes(~crc);
            Array.Reverse(ret);
            return ret;
        }

        /// **********************************************************************
        /// Name: CRC32/MPEG-2    x32+x26+x23+x22+x16+x12+x11+x10+x8+x7+x5+x4+x2+x+1
        /// Poly: 0x04C11DB7
        /// Init: 0xFFFFFFFF
        /// Refin: false
        /// Refout: false
        /// Xorout: 0x00000000
        ///*************************************************************************
        public byte[] CRC32_MPEG2(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            int length = buffer.Length;
            uint crc = 0xFFFFFFFF;// Initial value
            for (int i = 0; i < length; i++)
            {
                crc ^= (uint)(buffer[i] << 24);
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 0x80000000) > 0)
                        crc = (crc << 1) ^ 0x04C11DB7;
                    else
                        crc = crc << 1;
                }
            }
            byte[] ret = BitConverter.GetBytes(crc);
            Array.Reverse(ret);
            return ret;
        }

        // 纵向冗余校验(Longitudinal Redundancy Check,简称:LRC)是通信中常用的一种校验形式,也称LRC校验或纵向校验。 
        // 它是一种从纵向通道上的特定比特串产生校验比特的错误检测方法。在行列格式中(如磁带),LRC经常是与VRC一起使用,
        // 这样就会为每个字符校验码。在工业领域Modbus协议Ascii模式采用该算法。 LRC计算校验码,具体算法如下:
        /// 1、对需要校验的数据(2n个字符)两两组成一个16进制的数值求和。
        /// 2、将求和结果与256求模。
        /// 3、用256减去所得模值得到校验结果(另一种方法:将模值按位取反然后加1)。
        public byte[] LRC(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            byte lrc = 0;
            for (int i = 0; i < buffer.Length; i++)
            {
                lrc += buffer[i];
            }
            lrc = (byte)((lrc ^ 0xFF) + 1);
            return new byte[] { lrc };
        }

        // BCC(Block Check Character/信息组校验码),因校验码是将所有数据异或得出,故俗称异或校验。
        // 具体算法是:将每一个字节的数据(一般是两个16进制的字符)进行异或后即得到校验码。
        public byte[] BCC(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            byte bcc = 0;// Initial value
            for (int i = 0; i < buffer.Length; i++)
            {
                bcc ^= buffer[i];
            }
            return new byte[] { bcc };
        }

        /// <summary>
        /// 和校验
        /// </summary>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public byte[] ADD(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0) return null;
            int value = 0;
            for (int i = 0; i < buffer.Length; i++)
            {
                value += buffer[i];
            }
            byte bcc = (byte)(value & 0x00FF);
            return new byte[] { bcc };
        }
    }
}


private void button1_Click(object sender, EventArgs e)
{
    byte[] Data = Encoding.UTF8.GetBytes(textBox1.Text);
    if (Data == null) return;
    byte[] a = null;

    if (comboBox1.SelectedIndex == 0) a = new crcs().CRC4_ITU(Data);
    if (comboBox1.SelectedIndex == 1) a = new crcs().CRC5_EPC(Data);
    if (comboBox1.SelectedIndex == 2) a = new crcs().CRC5_ITU(Data);
    if (comboBox1.SelectedIndex == 3) a = new crcs().CRC5_USB(Data);
    if (comboBox1.SelectedIndex == 4) a = new crcs().CRC6_ITU(Data);
    if (comboBox1.SelectedIndex == 5) a = new crcs().CRC7_MMC(Data);
    if (comboBox1.SelectedIndex == 6) a = new crcs().CRC8(Data);
    if (comboBox1.SelectedIndex == 7) a = new crcs().CRC8_ITU(Data);
    if (comboBox1.SelectedIndex == 8) a = new crcs().CRC8_ROHC(Data);
    if (comboBox1.SelectedIndex == 9) a = new crcs().CRC8_MAXIM(Data);
    if (comboBox1.SelectedIndex == 10) a = new crcs().CRC16_IBM(Data);
    if (comboBox1.SelectedIndex == 11) a = new crcs().CRC16_MAXIM(Data);
    if (comboBox1.SelectedIndex == 12) a = new crcs().CRC16_USB(Data);
    if (comboBox1.SelectedIndex == 13) a = new crcs().CRC16_MODBUS(Data);
    if (comboBox1.SelectedIndex == 14) a = new crcs().CRC16_CCITT(Data);
    if (comboBox1.SelectedIndex == 15) a = new crcs().CRC16_CCITT_FALSE(Data);
    if (comboBox1.SelectedIndex == 16) a = new crcs().CRC16_X25(Data);
    if (comboBox1.SelectedIndex == 17) a = new crcs().CRC16_XMODEM(Data);
    if (comboBox1.SelectedIndex == 18) a = new crcs().CRC16_DNP(Data);
    if (comboBox1.SelectedIndex == 19) a = new crcs().CRC32(Data);
    if (comboBox1.SelectedIndex == 20) a = new crcs().CRC32_MPEG2(Data);
    if (comboBox1.SelectedIndex == 21) a = new crcs().LRC(Data);
    if (comboBox1.SelectedIndex == 22) a = new crcs().BCC(Data);
    if (comboBox1.SelectedIndex == 23) a = new crcs().ADD(Data);

    if (a != null) textBox2.Text = BitConverter.ToString(a).Replace("-", " ");
}