相关疑难解决方法(0)

如何从C#生成CRC-16

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

所需输出的屏幕截图.

我尝试过使用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 …
Run Code Online (Sandbox Code Playgroud)

c# crc crc16

12
推荐指数
2
解决办法
5万
查看次数

标签 统计

c# ×1

crc ×1

crc16 ×1