C#字节搜索

字节搜索

该系列方法用于检索字节的开头和结尾

using System;

class ByteArraySearch
{
    public static void Main()
    {
        // 示例:长字节数组
        byte[] sourceArray = new byte[] {
            // ... 其他数据 ...
            0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 
            // 假设这是我们要找的子数组的开头
            0x01, 0x02, 0x03, 0x04, 
            // ... 子数组内容 ...
            0x55, 0x55, 
            // 假设这是我们要找的子数组的结尾
            0xAA, 0xBB
            // ... 其他数据 ...
        };

        // 要查找的开头和结尾字节数组
        byte[] startSequence = new byte[] { 0x01, 0x02, 0x03, 0x04 };
        byte[] endSequence = new byte[] { 0xAA, 0xBB };

        // 查找并打印匹配的子数组的起始索引(如果找到)
        int startIndex = FindSubArrayWithStartAndEnd(sourceArray, startSequence, endSequence);
        if (startIndex != -1)
        {
            Console.WriteLine($"找到了匹配的子数组,起始索引为: {startIndex}");
            // 计算子数组的结束索引
            int endIndex = startIndex + startSequence.Length + GetSubArrayLengthAfterStart(sourceArray, startIndex, endSequence);
            // 打印匹配的子数组
            Console.WriteLine($"匹配的子数组为: {BitConverter.ToString(sourceArray, startIndex, endIndex - startIndex)}");
        }
        else
        {
            Console.WriteLine("没有找到匹配的子数组。");
        }
    }

    // 寻找具有特定开头和结尾的子数组的起始索引
    static int FindSubArrayWithStartAndEnd(byte[] source, byte[] startSeq, byte[] endSeq)
    {
        for (int i = 0; i <= source.Length - startSeq.Length - endSeq.Length; i++)
        {
            bool startMatch = true, endMatch = true;

            // 检查开头是否匹配
            for (int j = 0; j < startSeq.Length; j++)
            {
                if (source[i + j] != startSeq[j])
                {
                    startMatch = false;
                    break;
                }
            }

            // 如果开头匹配,检查结尾是否匹配
            if (startMatch)
            {
                for (int k = 0; k < endSeq.Length; k++)
                {
                    if (source[i + startSeq.Length + k] != endSeq[k])
                    {
                        endMatch = false;
                        break;
                    }
                }
            }

            if (startMatch && endMatch)
            {
                return i;
            }
        }
        return -1; // 没有找到匹配
    }

    // 给定起始索引后,计算直到特定结尾序列的子数组长度
    static int GetSubArrayLengthAfterStart(byte[] array, int startIndex, byte[] endSequence)
    {
        int count = 0;
        bool endFound = false;
        for (int i = startIndex; i < array.Length; i++)
        {
            if (!endFound && i + endSequence.Length <= array.Length)
            {
                endFound = true;
                for (int j = 0; j < endSequence.Length; j++)
                {
                    if (array[i + j] != endSequence[j])
                    {
                        endFound = false;
                        break;
                    }
                }
                if (endFound)
                {
                    return count + endSequence.Length;
                }
            }
            count++;
        }
        return count;
    }
}
posted @ 2024-06-19 13:56  冬天之雪  阅读(19)  评论(0编辑  收藏  举报