c# - 如何从 C# 生成 CRC-16

标签 c# crc crc16

我正在尝试使用 C# 生成 CRC-16。我用于 RS232 的硬件要求输入字符串为十六进制。下面的屏幕截图显示了正确的转换,对于测试,我需要 8000 为 0xC061,但是生成 CRC-16 的 C# 方法必须能够转换任何给定的 HEX 字符串。

Screenshot of required output.

我试过使用 Nito.KitchenSink.CRC

我还尝试了下面的方法,当输入 8000 时生成 8009 -

public string CalcCRC16(string strInput)
    {
        ushort crc = 0x0000;
        byte[] data = GetBytesFromHexString(strInput);
        for (int i = 0; i < data.Length; i++)
        {
            crc ^= (ushort)(data[i] << 8);
            for (int j = 0; j < 8; j++)
            {
                if ((crc & 0x8000) > 0)
                    crc = (ushort)((crc << 1) ^ 0x8005);
                else
                    crc <<= 1;
            }
        }
        return crc.ToString("X4");
    }

    public 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)
            {
                MessageBox.Show(ex.Message);
            }
        }
        return bytArOutput;
    }

最佳答案

开始吧;请注意,这是 CRC-16 的特定风格 - 仅说“CRC-16”会造成混淆。这从 http://www.sanity-free.com/ 中借用了一些实现细节- 请注意,我已将其设为static 而不是基于实例。

using System;

static class Program
{
    static void Main()
    {
        string input = "8000";
        var bytes = HexToBytes(input);
        string hex = Crc16.ComputeChecksum(bytes).ToString("x2");
        Console.WriteLine(hex); //c061
    }
    static byte[] HexToBytes(string input)
    {
        byte[] result = new byte[input.Length / 2];
        for(int i = 0; i < result.Length; i++)
        {
            result[i] = Convert.ToByte(input.Substring(2 * i, 2), 16);
        }
        return result;
    }

    public static class Crc16
    {
        const ushort polynomial = 0xA001;
        static readonly ushort[] table = new ushort[256];

        public static ushort ComputeChecksum(byte[] bytes)
        {
            ushort crc = 0;
            for (int i = 0; i < bytes.Length; ++i)
            {
                byte index = (byte)(crc ^ bytes[i]);
                crc = (ushort)((crc >> 8) ^ table[index]);
            }
            return crc;
        }

        static Crc16()
        {
            ushort value;
            ushort temp;
            for (ushort i = 0; i < table.Length; ++i)
            {
                value = 0;
                temp = i;
                for (byte j = 0; j < 8; ++j)
                {
                    if (((value ^ temp) & 0x0001) != 0)
                    {
                        value = (ushort)((value >> 1) ^ polynomial);
                    }
                    else
                    {
                        value >>= 1;
                    }
                    temp >>= 1;
                }
                table[i] = value;
            }
        }
    }
}

关于c# - 如何从 C# 生成 CRC-16,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22860356/

相关文章:

c - 如何匹配适用于各种数据大小的 CRC32 算法

c# - 缺少什么CRC-CCITT(0xFFFF)?

c# - Facebook 风格的 ASP.NET 按钮

c# - 与模型状态错误消息相对的成功消息

php - 将用 C 编写的 8 位 CRC 函数转换为 PHP

crc - 证明 CRC 的线性

c# - 在 Cake (C#) 中获取 TeamCity (Docker Linux) 参数或系统属性不返回任何内容

c# - 在 UI 线程上创建和启动任务

algorithm - 如何避免crc16冲突?

python - 如何在 Python 中将字符串转换为字节(已关闭)