找到另一个阵列里的数组(字节[])? [英] Find an array (byte[]) inside another array?

查看:118
本文介绍了找到另一个阵列里的数组(字节[])?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是找到另一个字节[]内一个byte []最简单的方法?我有一种感觉,我可以使用LINQ做到这一点,但我不知道怎么办。

What is the simplest way to find a byte[] inside another byte[]? i have a feeling i could do it with linq but i dont know how.

请注意:我做了一个搜索 [C#] ,并没有发现任何东西,我很惊讶。

Note: I did a search with the [c#] and didnt find anything, i am surprised.

推荐答案

下面是一个简单的(幼稚?)的方式来做到这一点:

Here's a simple (naive?) way to do it:

static int search(byte[] haystack, byte[] needle)
{
    for (int i = 0; i <= haystack.Length - needle.Length; i++)
    {
        if (match(haystack, needle, i))
        {
            return i;
        }
    }
    return -1;
}

static bool match(byte[] haystack, byte[] needle, int start)
{
    if (needle.Length + start > haystack.Length)
    {
        return false;
    }
    else
    {
        for (int i = 0; i < needle.Length; i++)
        {
            if (needle[i] != haystack[i + start])
            {
                return false;
            }
        }
        return true;
    }
}

这篇关于找到另一个阵列里的数组(字节[])?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆