视频教程地址 https://www.bilibili.com/video/BV1UL411b7L5/
以下是调用
public byte[] GetBytePart(byte[] data, byte[] startData, byte[] endData, bool contain = false) { int start = IndexOf(data, startData); int end = IndexOf(data, endData); if (start != -1 && end != -1) { int count = 0; if (contain) { count = end + endData.Length - start; } else { start = start + startData.Length; count = end - start; } return data.Skip(start).Take(count).ToArray(); } else { return null; } } public int IndexOf(byte[] data, byte[] find) { for (int i = 0; i < data.Length - find.Length; i++) { if (data.Skip(i).Take(find.Length).SequenceEqual(find)) { return i; } } return -1; } //原创来自http://www.luofenming.com/show.aspx?id=ART2018100900001,如转载请保留此地址
以下是调用
//这是截取 byte[] { 0x11, 0x21, 0x31, 0xff, 0x10, 0x01, 0x12, 0x13, 0x0a, 0x20, 0x11 ,0x02} //byte[] { 0x31, 0xff, 0x10 }, byte[] { 0x0a, 0x20 }这两个字节数组之间的字节并且包含这两个字节数据 byte[] bytes = new byte[] { 0x11, 0x21, 0x31, 0xff, 0x10, 0x01, 0x12, 0x13, 0x0a, 0x20, 0x11 ,0x02}; byte[] b = GetBytePart(bytes, new byte[] { 0x31, 0xff, 0x10 }, new byte[] { 0x0a, 0x20 }); //得到的结果是 0x01, 0x12, 0x13