namespace GUI { public static class CheckUtil { public static void Func_sum_check(byte[] data, int offset, int len, byte[] check) { int i; ushort result = 0; for (i = 0; i < len; i++) { result += data[i + offset]; } check[0] = (byte)result; check[1] = (byte)(result >> 8); } public static void Func_Xor_check(byte[] data, int offset, int len, out byte check) { int i; check = data[offset]; for (i = 1; i < len; i++) { check ^= data[i + offset]; } } public static bool ArrayEquals(this T[] src, T[] other, int offset = 0) { for (int i = 0; i < src.Length; i++) if (!src[i].Equals(other[i + offset])) return false; return true; } public static T[] ArrayCombine(this T[] src, T[] other) { T[] newT = new T[src.Length + other.Length]; for (int i = 0; i < src.Length; i++) newT[i] = src[i]; for (int i = 0; i < other.Length; i++) newT[i + src.Length] = other[i]; return newT; } public static bool CheckData(this byte[] data) { byte[] check2 = new byte[2]; Func_sum_check(data, 0, data.Length - 3, check2); if (!check2.ArrayEquals(data, data.Length - 3)) return false; Func_Xor_check(data, 0, data.Length - 1, out byte check); if (check != data[^1]) return false; return true; } public static byte[] PackData(this byte[] data) { byte[] check2 = new byte[2]; Func_sum_check(data, 0, data.Length, check2); byte[] withSumCheck = data.ArrayCombine(check2); Func_Xor_check(withSumCheck, 0, withSumCheck.Length, out byte last); return withSumCheck.ArrayCombine(new byte[1] { last }); } public static byte t(this bool b) => (byte)(b ? 1 : 0); } }