视频教程地址 https://www.bilibili.com/video/BV1UL411b7L5/
以下是调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | 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,如转载请保留此地址 |
以下是调用
1 2 3 4 5 6 7 8 | //这是截取 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 |