CRC校验函数

2019-09-19 08:28发布

采用多项式X16 + X15 + X2 + 1的CRC16校验,低字节在前,高字节在后,如何编写校验函数?谢谢!

标签: crc 校验
1条回答
Luminary・发光体
2楼-- · 2019-09-19 08:43

用了多年的,给你个参照:

namespace LModule.Utils
{
    public static class StringExt
    {

        public static string ToContentString(this Stream stream)
        {
            var reader = new StreamReader(stream);
            var text = reader.ReadToEnd();
            return text;
        }

        public static string ToBase64String(this Stream stream)
        {
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
            stream.Seek(0, SeekOrigin.Begin);
            return Convert.ToBase64String(bytes);
        }

        public static byte[] ToUtf8Buffer(this string content)
        {
            return Encoding.UTF8.GetBytes(content);
        }
        public static string ToISO_8859_1(this string srcText)
        {
            var dst = "";
            var src = srcText.ToCharArray();
            for (var i = 0; i < src.Length; i++)
            {
                var str = @"&#" + (int)src[i] + ";";
                dst += str;
            }
            return dst;
        }
        public static string FromISO_8859_1(this string srcText)
        {
            var dst = "";
            var src = srcText.Split(';');
            for (var i = 0; i < src.Length; i++)
            {
                if (src[i].Length > 0)
                {
                    var str = ((char)int.Parse(src[i].Substring(2))).ToString();
                    dst += str;
                }
            }
            return dst;
        }

        public static IList<byte> EscapeCode(this IList<byte> tmpbytes)
        {
            for (int i = 1; i < tmpbytes.Count - 1; i++)
            {
                if (tmpbytes[i] == 0x5b)
                {
                    tmpbytes[i] = (byte)0x5a;
                    tmpbytes.Insert(i + 1, (byte)0x01);
                    i++;
                }
                else if (tmpbytes[i] == 0x5a)
                {
                    tmpbytes[i] = (byte)0x5a;
                    tmpbytes.Insert(i + 1, (byte)0x02);
                    i++;
                }
                else if (tmpbytes[i] == 0x5d)
                {
                    tmpbytes[i] = (byte)0x5e;
                    tmpbytes.Insert(i + 1, (byte)0x01);
                    i++;
                }
                else if (tmpbytes[i] == 0x5e)
                {
                    tmpbytes[i] = (byte)0x5e;
                    tmpbytes.Insert(i + 1, (byte)0x02);
                    i++;
                }
            }
            return tmpbytes;
        }

        public static IList<byte> UnEscapeCode(this IList<byte> tmpbytes)
        {
            for (int i = 1; i < tmpbytes.Count - 1; i++)
            {
                if (tmpbytes[i] == (byte)0x5a && tmpbytes[i + 1] == (byte)0x01)
                {
                    tmpbytes[i] = (byte)0x5b;
                    tmpbytes.RemoveAt(i + 1);
                }
                else if (tmpbytes[i] == (byte)0x5a && tmpbytes[i + 1] == (byte)0x02)
                {
                    tmpbytes[i] = (byte)0x5a;
                    tmpbytes.RemoveAt(i + 1);
                }
                else if (tmpbytes[i] == (byte)0x5e && tmpbytes[i + 1] == (byte)0x01)
                {
                    tmpbytes[i] = (byte)0x5d;
                    tmpbytes.RemoveAt(i + 1);
                }
                else if (tmpbytes[i] == (byte)0x5e && tmpbytes[i + 1] == (byte)0x02)
                {
                    tmpbytes[i] = (byte)0x5e;
                    tmpbytes.RemoveAt(i + 1);
                }
            }
            return tmpbytes;
        }

        public static ushort CRC16_CCITT(this IList<byte> ucbuf, int offset, int iLen)
        {
            ushort crc = 0xFFFF;          // initial value
            ushort polynomial = 0x1021;   // 0001 0000 0010 0001  (0, 5, 12)

            for (int j = 0; j < iLen; ++j)
            {
                for (int i = 0; i < 8; i++)
                {
                    bool bit = ((ucbuf[j + offset] >> (7 - i) & 1) == 1);
                    bool c15 = ((crc >> 15 & 1) == 1);
                    crc <<= 1;
                    if (c15 ^ bit) crc ^= polynomial;
                }
            }

            crc &= 0xffff;

            return crc;
        }
        
        static readonly Random CodeRandom = new Random();
        private const int CharMin = 0X20;
        private const int CharMax = 0X7F;
        public static string GetGuidString(int len)//len must large than 15
        {
            var guidString = new StringBuilder();
            var dateChars = DateTime.Now.ToString("yyMMddHHmmssfff").ToCharArray();
            if(len < dateChars.Length)throw new Exception($"len must large than {dateChars.Length}");
            foreach (var t in dateChars) guidString.Append(t);
            for (var i = dateChars.Length-1; i < len; i++)guidString.Append((char)CodeRandom.Next(CharMin, CharMax));

            return guidString.ToString();
        }
        public static string GetRandomString(int len)
        {
            var randomString = new StringBuilder();
            if (len < 1) throw new Exception($"len must large than 0");
            for (var i = 0; i < len; i++) randomString.Append((char)CodeRandom.Next(CharMin, CharMax));

            return randomString.ToString();
        }
        public static string GetRandomLenthString(int minLen,int maxLen)
        {
            var len = CodeRandom.Next(minLen, maxLen);
            var randomString = new StringBuilder();
            if (len < 1) throw new Exception($"len must large than 0");
            for (var i = 0; i < len; i++) randomString.Append((char)CodeRandom.Next(CharMin, CharMax));

            return randomString.ToString();
        }
    }
}
查看更多
登录 后发表回答