.net字节搜索

# 字节搜索

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

~~~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;

    }

}

~~~

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值