毕业论文开发语言企业开发JAVA技术.NET技术WEB开发Linux/Unix数据库技术Windows平台移动平台嵌入式论文范文英语论文
您现在的位置: 毕业论文 >> net技术 >> 正文

crc16位校验码c#算法

更新时间:2013-5-26:  来源:毕业论文

crc16位校验码c#算法

举个例子
输入字节数组byte[]{ 47 78 00 01 00 00 00 BC 00 00 00 01 00 00 00 BC 00 02 54 D2}

得出校验码 0x6A6B
 #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

using System; namespace CRC  {   /// <summary>   /// CRC16 的摘要说明。   /// </summary>   public class CRC16:ICRC   {    #region CRC 16 位校验表         /// <summary>    /// 16 位校验表 Upper 表    /// </summary>    public ushort[] uppercrctab = new ushort[]     {      0x0000,0x1231,0x2462,0x3653,0x48c4,0x5af5,0x6ca6,0x7e97,      0x9188,0x83b9,0xb5ea,0xa7db,0xd94c,0xcb7d,0xfd2e,0xef1f     };   /// <summary>    /// 16 位校验表 Lower 表    /// </summary>    public ushort[] lowercrctab = new ushort[]     {      0x0000,0x1021,0x2042,0x3063,0x4084,0x50a5,0x60c6,0x70e7,      0x8108,0x9129,0xa14a,0xb16b,0xc18c,0xd1ad,0xe1ce,0xf1ef     };    #endregion   ushort crc = 0;   /// <summary>    /// 校验后的结果    /// </summary>    public long Value    {     get    {      return crc;     }     set    {      crc = (ushort)value;     }    }   /// <summary>    /// 设置crc 初始值    /// </summary>    public void Reset()    {     crc = 0;    }              /// <summary>    /// Crc16    /// </summary>    /// <param name="ucrc"></param>    /// <param name="buf"></param>    public void Crc(ushort ucrc,byte[] buf)    {     crc = ucrc;     Crc(buf);    }         /// <summary>    /// Crc16    /// </summary>    /// <param name="bval"></param>    public void Crc(int bval)    {     ushort h = (ushort)((crc >> 12) & 0x0f);     ushort l = (ushort)((crc >> 8 ) & 0x0f);     ushort temp = crc;     temp =(ushort)(((temp & 0x00ff) << 8) | bval);     temp =(ushort)(temp ^(uppercrctab[(h -1) + 1] ^ lowercrctab[(l - 1) + 1]));     crc = temp;    }     /// <summary>    /// Crc16    /// </summary>    /// <param name="buffer"></param>    public void Crc(byte[] buffer)    {     Crc(buffer,0,buffer.Length);    }   /// <summary>    /// Crc16    /// </summary>    /// <param name="buf"></param>    /// <param name="off"></param>    /// <param name="len"></param>    public void Crc(byte[] buf,int off,int len)    {     if (buf == null)     {      throw new ArgumentNullException("buf");     }           if (off < 0 || len < 0 || off + len > buf.Length)     {      throw new ArgumentOutOfRangeException();     }     for (int i = off; i < len ; i ++)     {      Crc(buf[i]);     }    }      }  }  分类

设为首页 | 联系站长 | 友情链接 | 网站地图 |

copyright©youerw.com 优尔论文网 严禁转载
如果本毕业论文网损害了您的利益或者侵犯了您的权利,请及时联系,我们一定会及时改正。