C#crc16校验和

来源:互联网 发布:淘宝上zara是真的吗 编辑:程序博客网 时间:2024/04/26 07:48

从网上搜了好久都没搞定,我老大一搜就好使,什么人品!


我的资源里有:http://download.csdn.net/detail/small5e4444/3724860

 

一下程序写在button事件里就行

 

 

                byte[] data = new byte[6];
                string[] st = textBox1.Text.Split(',');

                for (int i = 0; i < st.Length; i++)
                {
                    data[i] = Convert.ToByte(st[i]);
                }

 

                //上面是从 textbox1输入,例如 01,03,03,00,00,01

 

                //byte[] b = new byte[] { 01, 03, 03, 0x0a, 00, 02 };//手写 注意16进制数加 ox


                byte[] c = crc16(data);
                // Console.WriteLine(c);
                string xx = "";
                for (int i = 0; i < c.Length; i++)
                {
                    string x = Convert.ToString(c[i], 16);
                    xx += x;
                    textBox2.Text = xx;
                   // Console.Write(x);
                 //   Console.Write(" ");
                }
               // Console.ReadLine();

            }
            #region CRC16校验
            /// <summary>
            /// CRC16校验算法,低字节在前,高字节在后
            /// </summary>
            /// <param name="data">要校验的数组</param>
            /// <returns>返回校验结果,低字节在前,高字节在后</returns>
            public static int[] crc16(int[] data)
            {
                if (data.Length == 0)
                    throw new Exception("调用CRC16校验算法,(低字节在前,高字节在后)时发生异常,异常信息:被校验的数组长度为0。");
                int[] temdata = new int[data.Length + 2];
                int xda, xdapoly;
                int i, j, xdabit;
                xda = 0xFFFF;
                xdapoly = 0xA001;
                for (i = 0; i < data.Length; i++)
                {
                    xda ^= data[i];
                    for (j = 0; j < 8; j++)
                    {
                        xdabit = (int)(xda & 0x01);
                        xda >>= 1;
                        if (xdabit == 1)
                            xda ^= xdapoly;
                    }
                }
                temdata = new int[2] { (int)(xda & 0xFF), (int)(xda >> 8) };
                return temdata;
            }

            /// <summary>
            ///CRC16校验算法,(低字节在前,高字节在后)
            /// </summary>
            /// <param name="data">要校验的数组</param>
            /// <returns>返回校验结果,低字节在前,高字节在后</returns>
            public static byte[] crc16(byte[] data)
            {
                if (data.Length == 0)
                    throw new Exception("调用CRC16校验算法,(低字节在前,高字节在后)时发生异常,异常信息:被校验的数组长度为0。");
                byte[] temdata = new byte[data.Length + 2];
                int xda, xdapoly;
                byte i, j, xdabit;
                xda = 0xFFFF;
                xdapoly = 0xA001;
                for (i = 0; i < data.Length; i++)
                {
                    xda ^= data[i];
                    for (j = 0; j < 8; j++)
                    {
                        xdabit = (byte)(xda & 0x01);
                        xda >>= 1;
                        if (xdabit == 1)
                            xda ^= xdapoly;
                    }
                }
                temdata = new byte[2] { (byte)(xda & 0xFF), (byte)(xda >> 8) };
                return temdata;
            }
            #endregion

原创粉丝点击