byte[]数组的类型扩展方法:indexOf和split拆分
C#类型扩展方法:
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace iPublic.类型扩展方法 { /// <summary> /// 类型的扩展方法,用起来方便的 /// 修改记录: /// 20230415,海宏软件,zch,List的 /// </summary> public static class ExtendMethods { /// <summary> /// 扩展List<int>的tostring方法,注意,如果参数和基类一样,会调用基类的,这里就不执行了 /// </summary> /// <param name="list"></param> /// <param name="连接符"></param> /// <returns></returns> public static string ToString(this List<int> list, string 连接符) { if (list == null || list.Count < 1) return ""; string s = ""; foreach (var itm in list) s += 连接符 + itm.ToString(); return s; } /// <summary> /// 十六进制字符串转成byte数组,用空格、减号分割 /// </summary> /// <param name="InString"></param> /// <returns></returns> public static byte[] HexStringToByte(this string InString) { string[] ByteStrings; ByteStrings = InString.Split(' ', '-'); byte[] ByteOut; ByteOut = new byte[ByteStrings.Length]; for (int i = 0; i <= ByteStrings.Length - 1; i++) { ByteOut[i] = Byte.Parse(ByteStrings[i], System.Globalization.NumberStyles.HexNumber); } return ByteOut; } #region //IndexOfByte数组 /// <summary> /// 报告指定的 System.Byte[] 在此实例中的第一个匹配项的索引。 /// 参考:https://www.iteye.com/blog/testcs-dn-2099443 /// </summary> /// <param name="srcBytes">被执行查找的 System.Byte[]。</param> /// <param name="searchBytes">要查找的 System.Byte[]。</param> /// <returns>如果找到该字节数组,则为 searchBytes 的索引位置;如果未找到该字节数组,则为 -1。如果 searchBytes 为 null 或者长度为0,则返回值为 -1。</returns> public static int IndexOf(this byte[] srcBytes, byte[] searchBytes) { return IndexOf(srcBytes, searchBytes, 0); } public static int IndexOf(this byte[] srcBytes, byte[] searchBytes, int 开始位置) { if (srcBytes == null) { return -1; } if (searchBytes == null) { return -1; } if (srcBytes.Length == 0) { return -1; } if (searchBytes.Length == 0) { return -1; } if (srcBytes.Length < searchBytes.Length) { return -1; } int nSrc = srcBytes.Length, nSearch = searchBytes.Length; for (int i = 开始位置; i < nSrc /*- searchBytes.Length*/; i++) { if (srcBytes[i] != searchBytes[0]) continue; if (nSearch == 1) { return i; } if (i + nSearch > nSrc) return -1; //查找 bool flag = true; for (int j = 1; j < nSearch; j++) { if (srcBytes[i + j] != searchBytes[j]) { flag = false; break; } } if (flag) { return i; } } return -1; } #endregion #region //byte[].split拆分成多个数组 public static List<byte[]> split(this byte[] src, List<byte[]> separator) { List<byte[]> result = new List<byte[]>(); byte[] buf = null, bufAll = src; //Dictionary<int, int> indexs = new Dictionary<int, int>(); int nIdx = 0, nLastIdx = 0, nLen = src.Length; while (nLastIdx < src.Length) { byte[] finder = null; foreach (var itm in separator) { nIdx = src.IndexOf(itm, nLastIdx); //查找 if (nIdx >= 0) { finder = itm; break; } //找到了,记下来 } if (finder != null) { if (nIdx <=nLastIdx) result.Add(null); //第一位就是 else { //取出有效数据,放入结果中 buf = src.Skip(nLastIdx).Take(nIdx - nLastIdx).ToArray(); result.Add(buf); } nLastIdx = nIdx + finder.Length; //上次的位置 } else { //全部 if (src.Length > nLastIdx) { buf = src.Skip(nLastIdx).Take(src.Length - nLastIdx).ToArray(); result.Add(buf); } nLastIdx = src.Length; } } // return result; } #endregion } }
byte[]数组的indexOf和split比较好用.
用法:using iPublic.类型扩展方法;
然后:byte[] buf=new byte[]{0,1,2,3};
buf.indexOf(参数);
buf.split(参数)