private static void TestCRC8(CRC8Type type, params IEnumerable <byte>[] testValues) { foreach (var testValue in testValues) { Console.WriteLine("------------------------------------------------------------------"); Console.WriteLine($"CRC8 [{type}] test for source [{testValue.ToHexString()}]"); Console.WriteLine($"crc is 0x{testValue.CRC8(type).ToString("x2")}"); Console.WriteLine($"source + crc is 0x{testValue.WithCRC8(type).ToHexString()}"); Console.WriteLine("------------------------------------------------------------------"); } }
/*******************************************/ /// <summary> /// This function returns the CRC8 value. /// </summary> /// <param name="data"></param> /// <param name="type">This parameter means crc8 algorithm type.</param> /// <returns></returns> /*******************************************/ public static byte CRC8(this IEnumerable <byte> data, CRC8Type type = CRC8Type.Basic) { byte result = 0; if (type == CRC8Type.Basic) { result = data.ComputeNormalCRC8(); } else if (type == CRC8Type.Maxim) { result = data.ComputeMaximCRC8(); } // else if (type == CRC8Type.Unknown) result = data.ComputeTestCRC8(); return(result); }
/*******************************************/ /// <summary> /// This function returns the byte array included crc8 value. /// </summary> /// <param name="data"></param> /// <param name="type">This parameter means crc8 algorithm type.</param> /// <returns></returns> /*******************************************/ public static IEnumerable <byte> WithCRC8(this IEnumerable <byte> data, CRC8Type type) => data.Append(data.CRC8(type));