【问题标题】:C# CRC-16-CCITT 0x8408 Polynomial. Help neededC# CRC-16-CCITT 0x8408 多项式。需要帮助
【发布时间】:2016-01-22 17:34:23
【问题描述】:

我是通信编程的新手。基本上,我需要得到 CRC 输出的十六进制等效值。我有一个十六进制字符串,它是参数 -

EE0000000015202020202020202020202020323134373030353935

这是两个字符串的连接。我需要的输出是E6EB 中的hex59115 中的ushort。我根据在网上找到的内容尝试了不同的方法,但无济于事。我应该使用的多项式是0x8408,即[CRC-16-CCITT][1]http://en.wikipedia.org/wiki/Polynomial_representations_of_cyclic_redundancy_checks

我尝试了这种方法,CRC_CCITT Kermit 16 in C#,但输出不正确。我还尝试了按位 ~ 运算符作为反向计算的一些建议,但仍然失败。

非常感谢任何帮助。

【问题讨论】:

  • “我尝试了不同的方法”你尝试了什么?
  • 我所做的方法是在 C# 中的 CRC_CCITT Kermit 16 的链接中,stackoverflow.com/questions/7659286/…。您应该在取消投票之前访问过该链接。
  • 您应该更新您的问题,以准确解释您尝试了什么以及得到的结果。目前,不清楚您在问什么。

标签: crc16


【解决方案1】:

RevEng 报告:

% ./reveng -s -w 16 EE0000000015202020202020202020202020323134373030353935e6eb
width=16  poly=0x1021  init=0xffff  refin=true  refout=true  xorout=0xffff  check=0x906e  name="X-25"

这就是你的 CRC。请注意,CRC 是反射的,其中 0x84080x1021 反射的。

【讨论】:

    【解决方案2】:

    我找到了解决方案,我会发布它们以防有人遇到同样的问题。

    private ushort CCITT_CRC16(string strInput)
    {
            ushort data;
            ushort crc = 0xFFFF;
            byte[] bytes = GetBytesFromHexString(strInput);
            for (int j = 0; j < bytes.Length; j++)
            {
                crc = (ushort)(crc ^ bytes[j]);
                for (int i = 0; i < 8; i++)
                {
                    if ((crc & 0x0001) == 1)
                        crc = (ushort)((crc >> 1) ^ 0x8408);
                    else
                        crc >>= 1;
                }
            }
            crc = (ushort)~crc;
            data = crc;
            crc = (ushort)((crc << 8) ^ (data >> 8 & 0xFF));
            return crc;
    }
    
    private byte[] GetBytesFromHexString(string strInput)
    {
            Byte[] bytArOutput = new Byte[] { };
            if (!string.IsNullOrEmpty(strInput) && strInput.Length % 2 == 0)
            {
                SoapHexBinary hexBinary = null;
                try
                {
                    hexBinary = SoapHexBinary.Parse(strInput);
                    if (hexBinary != null)
                    {
                        bytArOutput = hexBinary.Value;
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            return bytArOutput;
    }
    

    为 SoapHexBinary 导入 System.Runtime.Remoting.Metadata.W3cXsd2001。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-20
      • 2014-10-04
      • 2011-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-01
      • 2016-03-09
      相关资源
      最近更新 更多