【总结】Byte[] 查找子数组位置
/// <summary>/// 查找子数组位置
/// </summary>
/// <param name="src">源数组</param>
/// <param name="find">查找数组</param>
/// <returns>返回位置</returns>
public static int FindBytes(byte[] src, byte[] find)
{
int index = -1;
int matchIndex = 0;
for (int i = 0; i < src.Length; i++)
{
if (src == find)
{
if (matchIndex == find.Length - 1)
{
index = i - matchIndex;
break;
}
matchIndex++;
}
else {
matchIndex = 0;
}
}
return index;
}
byte[] data = { 0x7e, 0x00, 0x01, 0x7e, 0x7d, 0x7e };
int index = FindBytes(data, new byte[] { 0x7d, 0x7e });
index = FindBytes(data, new byte[] { 0x7e, 0x00 });
index = FindBytes(data, new byte[] { 0x7e, 0x7d });
index = FindBytes(data, new byte[] {0x7e, 0x02});
页:
[1]