CRC-CCITT 标准CRC16(1021) 算法校验类

 最新新遇到设备采用CRC-CCITT 标准CRC16(1021),网上很多相关文章,但是大都结果不对。以下代码来自https://bbs.csdn.net/topics/390876846回答中的代码 

代码如下:

 public static String getCRC16_CCITT(String Source)
        {
            int crc = 0xFFFF;          // initial value
            int polynomial = 0x1021;   // 0001 0000 0010 0001  (0, 5, 12) 
            String tmp = "";
            byte[] bytes = new byte[Source.Length / 2];
            for (int i = 0; i < Source.Length - 1; i++)
            {
                if (i % 2 == 0)
                {
                    tmp = Source.Substring(i, 2);
                    bytes[i / 2] = (byte)Int16.Parse(tmp, System.Globalization.NumberStyles.HexNumber);
                }
            }
            foreach (byte b in bytes)
            {
                for (int i = 0; i < 8; i++)
                {
                    bool bit = ((b >> (7 - i) & 1) == 1);
                    bool c15 = ((crc >> 15 & 1) == 1);
                    crc <<= 1;
                    if (c15 ^ bit) crc ^= polynomial;
                }
            }
            crc &= 0xffff;
            string strDest = crc.ToString("X");
            return strDest;
        }

测试:

02383638343734303435383131393731301909062046375D1104400000

得出结果为:F5E3

另附CRC在线校验地址:https://www.lammertbies.nl/comm/info/crc-calculation.html

-------------------------------------------------------------------------------------------------------------------------------------------------------------

2019.09.16更新

基于上面的代码,做了一些扩展

public class CRC_CCITT_1021
    {
        /// <summary>
        /// CRC 校验
        /// </summary>
        /// <param name="Source">待校验数值</param>
        /// <returns>true为校验成功,false为校验失败</returns>
        public static bool CheckCrc(string Source)
        {
            return string.Compare(getCRC16_CCITT2(Source.Substring(0,Source.Length-4)), Source.Substring(Source.Length-4), true) == 0;
        }

        /// <summary>
        /// CRC 校验
        /// </summary>
        /// <param name="Source">待校验数值</param>
        /// <param name="result">校验结果</param>
        /// <returns>true为校验成功,false为校验失败</returns>
        public static bool CheckCrc(string Source,string result)
        {
            return string.Compare( getCRC16_CCITT2(Source),result,true)==0;
        }
        /// <summary>
        /// 计算CRC
        /// </summary>
        /// <param name="Source">源数据</param>
        /// <param name="offset">起始位置</param>
        /// <param name="length">数据长度</param>
        /// <returns>校验结果</returns>
        public static String getCRC16_CCITT(String Source, int offset, int length)
        {
            return getCRC16_CCITT2(Source.Substring(offset, length));
        }
        /// <summary>
        /// 计算CRC
        /// </summary>
        /// <param name="Source">源数据</param>
        /// <returns>校验结果</returns>
        public static String getCRC16_CCITT2(String Source)
        {
            int crc = 0xFFFF;          // initial value
            int polynomial = 0x1021;   // 0001 0000 0010 0001  (0, 5, 12) 
            String tmp = "";
            byte[] bytes = new byte[Source.Length / 2];
            for (int i = 0; i < Source.Length - 1; i++)
            {
                if (i % 2 == 0)
                {
                    tmp = Source.Substring(i, 2);
                    bytes[i / 2] = (byte)Int16.Parse(tmp, System.Globalization.NumberStyles.HexNumber);
                }
            }
            foreach (byte b in bytes)
            {
                for (int i = 0; i < 8; i++)
                {
                    bool bit = ((b >> (7 - i) & 1) == 1);
                    bool c15 = ((crc >> 15 & 1) == 1);
                    crc <<= 1;
                    if (c15 ^ bit) crc ^= polynomial;
                }
            }
            crc &= 0xffff;
            string strDest = crc.ToString("X");
            return strDest;
        }
    }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值