How to generate a CRC-16 from C#

后端 未结 2 1581
醉梦人生
醉梦人生 2020-12-01 08:21

I am trying to generate a CRC-16 using C#. The hardware I am using for RS232 requires the input string to be HEX. The screenshot below shows the correct conversion, For a te

相关标签:
2条回答
  • 2020-12-01 09:01

    In Addition, If you want CRC16-CCITT.

    private ushort Crc16Ccitt(byte[] bytes)
    {
        const ushort poly = 4129;
        ushort[] table = new ushort[256];
        ushort initialValue = 0xffff;
        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;
    }
    
    0 讨论(0)
  • 2020-12-01 09:12

    Here we go; note that this is a specific flavor of CRC-16 - it is confusing to say just "CRC-16". This borrows some implementation specifics from http://www.sanity-free.com/ - note I have made it static rather than instance-based.

    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;
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题