/* * 基本处理 * 肖俊 * 2015-4-2 */ using System.Text; namespace AllBase { public static class BaseWork { /// /// 两个数组是否相同 /// /// /// /// public static bool IsSame(ref byte[] b1, ref byte[] b2) { if (b1.Length != b2.Length) return false; for (int i = 0; i < b1.Length; i++) if (b1[i] != b2[i]) return false; return true; } /// /// 取得一个数组在另一个数组中的位置 /// /// 主数组 /// 查找数组 /// 起点 /// public static int GetIndexFirst(ref byte[] b1, ref byte[] b2, int start) { if (b1.Length < b2.Length || start > b1.Length - 1) return -1; int j; for (int i = start; i < b1.Length - b2.Length+1; i++) { if (b1[i] != b2[0]) continue; for (j = 1; j < b2.Length; j++) if (b1[i + j] != b2[j]) break; if (j == b2.Length) return i; } return -1; } /// /// 一个数组是否在另一个数组的结尾 /// /// 主数据 /// 结尾数组 /// public static bool IsEndByte(ref byte[] getByte, ref byte[] end) { if (end.Length > getByte.Length) return false; int i, c = getByte.Length - end.Length; for (i = 0; i < end.Length; i++) if (end[i] != getByte[c + i]) break; if (i == end.Length) return true; return false; } /// /// 取得一个数组在另一个数组中的位置 /// /// 主数组 /// 查找数组 /// public static int GetIndexLast(ref byte[] b1, ref byte[] b2) { if (b1.Length < b2.Length) return -1; int j, end = b2.Length - 1; for (int i = b1.Length - 1; i >= end; i--) { if (b1[i] != b2[end]) continue; for (j = 1; j < b2.Length; j++) if (b1[i - j] != b2[end - j]) break; if (j == b2.Length) return i - end; } return -1; } } }