C# CRC16算法

左右移运算 

左(<<),有(>>)

<< 左移运算 把第一个数进行左移指定第二个数位数,并且左移相当于乘以2的多少次方
>> 右移运算 把第一个数进行右移指定第二个数位数,并且右移相当于除以2的多少次方

通俗的说,位移的实现是将数据转换成二进制后,进行左右移动的。如果左移,则右边补零.

如果是右移,则是左边补零,后边溢出的则去掉。因此,左移可以理解为整数的乘法,而右移则是理解为整数的取整除法。

int num = 7;
int result = num >> 2; // 7 /(2*2) = 7 / 4 = 1

 Console.WriteLine(result);

 int a = 1;
 int b = a << 3; // 1* 2*2*2 = 8    
 Console.WriteLine(b);

CRCCalc16类

CRC16:即循环冗余校检码。数据通信当中一种常用的差错校检码

其特征信息字段和字段长度可以是任意选定的,对数据进行多项目计算

并且将得到的结果附加在帧的后面,接受的设备也执行类似的算法,以保证数据传输的正确性和完整性

crc-16过程?
        1 初始化一个16为的寄存地址 用作初始化
        2 遍历数据字节,从最高到最低
        3 将数据字节与寄存器异或      
        4 对寄存器进行8次迭代,每一次迭代将寄存器右移一位
        5 如果最低位位1,将寄存器与生产多项式0x8005异或,否则只进行右移操作
        6 重复上述步骤直到遍历完所有的字节
        7 最终寄存器的值就是crc16校检码

 public static byte[] CRCCalc(byte[] data)// []
 {
     //1 初始化一个16位的寄存器地址 用作初始值
     int crc = 0xffff;

     //2 遍历数据字节
     for (int i = 0; i < data.Length; i++)
     {
         //3 将数据字节与寄存器异或
         crc = crc ^ data[i];
         //4 对寄存器进行8次迭代,每一次迭代讲寄存器右移一位
         for (int j = 0; j < 8; j++)
         {
             int temp;
             temp = crc & 1;
             crc = crc >> 1;//每一次迭代将寄存器右移一位
             crc = crc & 0x7fff;
             //5 如果最低位位1,将寄存器与生成多项式0x8005异或,否则只进行右移操作
             if (temp == 1)
             {
                 crc = crc ^ 0xa001;
             }
             crc = crc & 0xffff;
         }
     }
     byte[] crc16 = new byte[2];// crc寄存器的高低为进行互换
     crc16[1] = (byte)((crc >> 8) & 0xff); //crc寄存器高八位变成了八低位
     crc16[0] = (byte)(crc & 0xff);// crc寄存器低八位变成了高低位
     return crc16;

 }
 /// <summary>
 /// CRC校验,参数为空格或逗号间隔的字符串
 /// </summary>
 /// <param name="data">校验数据,逗号或空格间隔的16进制字符串(带有0x或0X也可以),逗号与空格不能混用</param>
 /// <returns>字节0是高8位,字节1是低8位</returns>
 public static byte[] CRCCalc(string data)
 {
     //分隔符是空格还是逗号进行分类,并去除输入字符串中的多余空格
     IEnumerable<string> datac = data.Contains(",") ? data.Replace(" ", "").Replace("0x", "").Replace("0X", "").Trim().Split(',') : data.Replace("0x", "").Replace("0X", "").Split(' ').ToList().Where(u => u != "");
     List<byte> bytedata = new List<byte>();
     foreach (string str in datac)
     {
         bytedata.Add(byte.Parse(str, System.Globalization.NumberStyles.AllowHexSpecifier));
     }
     byte[] crcbuf = bytedata.ToArray();
     //crc计算赋初始值
     return CRCCalc(crcbuf);
 }

 /// <summary>
 ///  CRC校验,截取data中的一段进行CRC16校验
 /// </summary>
 /// <param name="data">校验数据,字节数组</param>
 /// <param name="offset">从头开始偏移几个byte</param>
 /// <param name="length">偏移后取几个字节byte</param>
 /// <returns>字节0是高8位,字节1是低8位</returns>
 public static byte[] CRCCalc(byte[] data, int offset, int length)
 {
     byte[] Tdata = data.Skip(offset).Take(length).ToArray();
     return CRCCalc(Tdata);
 }

XOR异或运算   ^

①0^0=0,   0^1=1       0异或任何数=任何数

②1 ^ 0 = 1,1 ^ 1 = 0  1异或任何数=任何数取反

③1 ^ 1 = 0,0 ^ 0 = 0  任何数异或自己=把自己置0

AND & 与运算

1&0=0; 1&1=1;   0&1=0;   0&0=0

&& 逻辑运算,true&&true= true;  其余都为false

OR |  或运算

 1|0=1 ; 1|1=1; 0|1=1;0|0=0
|| 逻辑运算 或

按钮:

 private void button1_Click(object sender, EventArgs e)
 {
     // 1 传递的是字节数组类型
     byte[] data = new byte[] { 0x01, 0x03, 0x00, 0x03, 0x00, 0x01 };
     byte[] crc = CRCCalc16.CRCCalc(data);
     for (int i = 0; i < crc.Length; i++)
     {
         Console.WriteLine(crc[i]); //十进制116 10  //16进制是74 0A
     }

     //2传递的字符串类型 
     byte[] crc1 = CRCCalc16.CRCCalc("01 03 00 03 00 01");
     for (int i = 0; i < crc1.Length; i++)
     {
         Console.WriteLine(crc1[i]); //十进制116 10  //16进制是74 0A
     }

     //3传递是字节数组和 字节开始位置和结束位置
     byte[] crc2 = CRCCalc16.CRCCalc(data, 0, data.Length);
     for (int i = 0; i < crc2.Length; i++)
     {
         Console.WriteLine(crc2[i]); //十进制116 10  //16进制是74 0A
     }

 }
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CRC32(循环冗余校验)算法是一种常用的数据校验算法,它可以用于检测数据传输过程中是否出现了错误。 在 C# 中,可以使用 System.Security.Cryptography 命名空间下的 CRC32 类来实现 CRC32 算法。下面是一个简单的示例代码: ```csharp using System; using System.Security.Cryptography; public class CRC32Example { public static string GetCRC32(string input) { byte[] bytes = System.Text.Encoding.UTF8.GetBytes(input); using (var crc32 = new CRC32()) { byte[] hash = crc32.ComputeHash(bytes); return BitConverter.ToString(hash).Replace("-", "").ToLower(); } } } public class CRC32 : HashAlgorithm { private const uint DefaultPolynomial = 0xedb88320u; private const uint DefaultSeed = 0xffffffffu; private uint hash; private readonly uint seed; private readonly uint[] table; private static uint[] defaultTable; public CRC32() { table = InitializeTable(DefaultPolynomial); seed = DefaultSeed; Initialize(); } public CRC32(uint polynomial, uint seed) { table = InitializeTable(polynomial); this.seed = seed; Initialize(); } public override void Initialize() { hash = seed; } protected override void HashCore(byte[] array, int ibStart, int cbSize) { hash = CalculateHash(table, hash, array, ibStart, cbSize); } protected override byte[] HashFinal() { byte[] hashBuffer = UInt32ToBigEndianBytes(~hash); this.HashValue = hashBuffer; return hashBuffer; } public override int HashSize => 32; private static uint[] InitializeTable(uint polynomial) { if (polynomial == DefaultPolynomial && defaultTable != null) return defaultTable; uint[] createTable = new uint[256]; for (int i = 0; i < 256; i++) { uint entry = (uint)i; for (int j = 0; j < 8; j++) if ((entry & 1) == 1) entry = (entry >> 1) ^ polynomial; else entry = entry >> 1; createTable[i] = entry; } if (polynomial == DefaultPolynomial) defaultTable = createTable; return createTable; } private static uint CalculateHash(uint[] table, uint seed, byte[] buffer, int start, int size) { uint crc = seed; for (int i = start; i < size; i++) unchecked { crc = (crc >> 8) ^ table[buffer[i] ^ crc & 0xff]; } return crc; } private static byte[] UInt32ToBigEndianBytes(uint x) { return new byte[] { (byte)((x >> 24) & 0xff), (byte)((x >> 16) & 0xff), (byte)((x >> 8) & 0xff), (byte)(x & 0xff) }; } } ``` 使用示例: ```csharp string input = "Hello, World!"; string crc32 = CRC32Example.GetCRC32(input); Console.WriteLine("CRC32 校验值为:" + crc32); ``` 输出结果: ``` CRC32 校验值为:6f2f7a8c ``` 以上代码实现了一个简单的 CRC32 算法,可以用于计算任意字符串的校验值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值