在另一个数组中找到一个数组 (byte[])? [英] Find an array (byte[]) inside another array?

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

问题描述

在另一个 byte[] 中找到一个 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;
    }
}

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

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