C# CRC16 CCITT XModem

 

核心方法,如果要返回两个字节,则把最后改为 return BitConverter.GetBytes(crc)

        private ushort Crc16Ccitt(byte[] bytes)
         {
            ushort poly = 0x1021;
            ushort[] table = new ushort[256];
            ushort initialValue = 0x0;
            ushort temp, a;
            ushort crc = initialValue;
            for (int i = 0; i < table.Length; ++i)
            {
                temp = 0;
                a = (ushort)(i << 8);
                for (int j = 0; j < 8; ++j)
                {
                    if (((temp ^ a) & 0x8000) != 0)
                        temp = (ushort)((temp << 1) ^ poly);
                    else
                        temp <<= 1;
                    a <<= 1;
                }
                table[i] = temp;
            }
            for (int i = 0; i < bytes.Length; ++i)
            {
                crc = (ushort)((crc << 8) ^ table[((crc >> 8) ^ (0xff & bytes[i]))]);
            }
            return crc;
        }

 

提取为类,把查询表提前初始化,避免每次调用都进行计算。

 

    /// <summary>
    /// CRC16 CCITT XModem计算校验值
    /// </summary>
    public static class CRC16
    {
        /// <summary>
        /// 多项式
        /// </summary>
        const ushort Polynominal = 0x1021;

        /// <summary>
        /// 查询表
        /// </summary>
        static ushort[] table = new ushort[256];

        /// <summary>
        /// 静态构造函数,初始化查询表
        /// </summary>
        static CRC16()
        {
            ushort temp, a;
            for (int i = 0; i < table.Length; ++i)
            {
                temp = 0;
                a = (ushort)(i << 8);
                for (int j = 0; j < 8; ++j)
                {
                    if (((temp ^ a) & 0x8000) != 0)
                        temp = (ushort)((temp << 1) ^ Polynominal);
                    else
                        temp <<= 1;
                    a <<= 1;
                }
                table[i] = temp;
            }
        }

        /// <summary>
        /// 计算校验值
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] CheckSum(List<byte> data)
        {
            ushort crc = 0x0;

            for (int i = 0; i < data.Count; ++i)
            {
                crc = (ushort)((crc << 8) ^ table[((crc >> 8) ^ (0xff & data[i]))]);
            }
            return BitConverter.GetBytes(crc);
        }

        /// <summary>
        /// 计算部分区域的校验值
        /// </summary>
        /// <param name="data"></param>
        /// <param name="startIndex"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static byte[] CheckSumRange(List<byte> data, int startIndex, int count)
        {
            return CheckSum(data.GetRange(startIndex, count));
        }
    }

 

参考资料:

CRC16算法

维基百科

在线计算CRC值

 

转载于:https://www.cnblogs.com/xyz0835/p/9598079.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值