用C#实现的几种常用数据校验方法整理(CRC校验;LRC校验;BCC校验;累加和校验)...

            CRC即循环冗余校验码(Cyclic Redundancy Check):是数据通信领域中最常用的一种查错校验码,其特征是信息字段和校验字段的长度可以任意选定。循环冗余检查(CRC)是一种数据传输检错功能,对数据进行多项式计算,并将得到的结果附在帧的后面,接收设备也执行类似的算法,以保证数据传输的正确性和完整性。
        /// CRC算法参数模型解释:
        /// NAME:参数模型名称。
        ///WIDTH:宽度,即CRC比特数。
        /// POLY:生成项的简写,以16进制表示。例如:CRC-32即是0x04C11DB7,忽略了最高位的"1",即完整的生成项是0x104C11DB7。
        ///INIT:这是算法开始时寄存器(crc)的初始化预置值,十六进制表示。
        ///REFIN:待测数据的每个字节是否按位反转,True或False。
        /// REFOUT:在计算后之后,异或输出之前,整个数据是否按位反转,True或False。
        /// XOROUT:计算结果与此参数异或后得到最终的CRC值。

  1  /// **********************************************************************
  2         /// Name: CRC-4/ITU    x4+x+1
  3         /// Poly: 0x03
  4         /// Init: 0x00
  5         /// Refin: true
  6         /// Refout: true
  7         /// Xorout: 0x00    
  8         ///*************************************************************************
  9         public static byte[] Crc1(byte[] buffer, int start = 0, int len = 0)
 10         {
 11             if (buffer == null || buffer.Length == 0) return null;
 12             if (start < 0) return null;
 13             if (len == 0) len = buffer.Length - start;
 14             int length = start + len;
 15             if (length > buffer.Length) return null;
 16             byte crc = 0;// Initial value 
 17             for (int i = start; i < length; i++)
 18             {
 19                 crc ^= buffer[i];
 20                 for (int j = 0; j < 8; j++)
 21                 {
 22                     if ((crc & 1) > 0)
 23                         crc = (byte)((crc >> 1) ^ 0x0C);//0x0C = (reverse 0x03)>>(8-4)
 24                     else
 25                         crc = (byte)(crc >> 1);
 26                 }
 27             }
 28             return new byte[] { crc };
 29         }
 30     /// **********************************************************************
 31     /// Name: CRC-5/EPC    x5+x3+1
 32     /// Poly: 0x09
 33     /// Init: 0x09
 34     /// Refin: false
 35     /// Refout: false
 36     /// Xorout: 0x00    
 37     ///*************************************************************************
 38         public static byte[] Crc2(byte[] buffer, int start = 0, int len = 0)
 39         {
 40             if (buffer == null || buffer.Length == 0) return null;
 41             if (start < 0) return null;
 42             if (len == 0) len = buffer.Length - start;
 43             int length = start + len;
 44             if (length > buffer.Length) return null;
 45             byte crc = 0x48;// Initial value: 0x48 = 0x09<<(8-5)
 46             for (int i = start; i < length; i++)
 47             {
 48                 crc ^= buffer[i];
 49                 for (int j = 0; j < 8; j++)
 50                 {
 51                     if ((crc & 0x80) > 0)
 52                         crc = (byte)((crc << 1) ^ 0x48);// 0x48 = 0x09<<(8-5)
 53                     else
 54                         crc = (byte)(crc << 1);
 55                 }
 56             }
 57             return new byte[] { (byte)(crc >> 3) };
 58         }
 59     /// **********************************************************************
 60     /// Name: CRC-5/ITU    x5+x4+x2+1
 61     /// Poly: 0x15
 62     /// Init: 0x00
 63     /// Refin: true
 64     /// Refout: true
 65     /// Xorout: 0x00    
 66     ///*************************************************************************
 67         public static byte[] Crc3(byte[] buffer, int start = 0, int len = 0)
 68         {
 69             if (buffer == null || buffer.Length == 0) return null;
 70             if (start < 0) return null;
 71             if (len == 0) len = buffer.Length - start;
 72             int length = start + len;
 73             if (length > buffer.Length) return null;
 74             byte crc = 0;// Initial value
 75             for (int i = start; i < length; i++)
 76             {
 77                 crc ^= buffer[i];
 78                 for (int j = 0; j < 8; j++)
 79                 {
 80                     if ((crc & 1) > 0)
 81                         crc = (byte)((crc >> 1) ^ 0x15);// 0x15 = (reverse 0x15)>>(8-5)
 82                     else
 83                         crc = (byte)(crc >> 1);
 84                 }
 85             }
 86             return new byte[] { crc  };
 87         }
 88     /// **********************************************************************
 89     /// Name: CRC-5/USB    x5+x2+1
 90     /// Poly: 0x05
 91     /// Init: 0x1F
 92     /// Refin: true
 93     /// Refout: true
 94     /// Xorout: 0x1F    
 95     ///*************************************************************************
 96         public static byte[] Crc4(byte[] buffer, int start = 0, int len = 0)
 97         {
 98             if (buffer == null || buffer.Length == 0) return null;
 99             if (start < 0) return null;
100             if (len == 0) len = buffer.Length - start;
101             int length = start + len;
102             if (length > buffer.Length) return null;
103             byte crc = 0x1F;// Initial value
104             for (int i = start; i < length; i++)
105             {
106                 crc ^= buffer[i];
107                 for (int j = 0; j < 8; j++)
108                 {
109                     if ((crc & 1) > 0)
110                         crc = (byte)((crc >> 1) ^ 0x14);// 0x14 = (reverse 0x05)>>(8-5)
111                     else
112                         crc = (byte)(crc >> 1);
113                 }
114             }
115             return new byte[] {(byte)( crc ^ 0x1F)  };
116         }
117     /// **********************************************************************
118     /// Name: CRC-6/ITU    x6+x+1
119     /// Poly: 0x03
120     /// Init: 0x00
121     /// Refin: true
122     /// Refout: true
123     /// Xorout: 0x00    
124     ///*************************************************************************
125         public static byte[] Crc5(byte[] buffer, int start = 0, int len = 0)
126         {
127             if (buffer == null || buffer.Length == 0) return null;
128             if (start < 0) return null;
129             if (len == 0) len = buffer.Length - start;
130             int length = start + len;
131             if (length > buffer.Length) return null;
132             byte crc = 0;// Initial value
133             for (int i = start; i < length; i++)
134             {
135                 crc ^= buffer[i];
136                 for (int j = 0; j < 8; j++)
137                 {
138                     if ((crc & 1) > 0)
139                         crc = (byte)((crc >> 1) ^ 0x30);// 0x30 = (reverse 0x03)>>(8-6)
140                     else
141                         crc = (byte)(crc >> 1);
142                 }
143             }
144             return new byte[] { crc  };
145         }
146     /// **********************************************************************
147     /// Name: CRC-7/MMC    x7+x3+1
148     /// Poly: 0x09
149     /// Init: 0x00
150     /// Refin: false
151     /// Refout: false
152     /// Xorout: 0x00    
153     ///*************************************************************************
154         public static byte[] Crc6(byte[] buffer, int start = 0, int len = 0)
155         {
156             if (buffer == null || buffer.Length == 0) return null;
157             if (start < 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值