Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 检查一个字节[]是否包含在另一个字节[]中的最佳方法_C#_.net 4.0 - Fatal编程技术网

C# 检查一个字节[]是否包含在另一个字节[]中的最佳方法

C# 检查一个字节[]是否包含在另一个字节[]中的最佳方法,c#,.net-4.0,C#,.net 4.0,可能重复: 你好, 如果一个字节[]在另一个字节[]中,最好的搜索方法是什么 比如说 byte[] first = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05 }; byte[] second = new byte[] { 0x01, 0x02 }; byte[] third = new byte[] { 0x01, 0x03 }; 该方法将返回: first.Contains(second); // true first.Contains(third

可能重复:

你好,

如果一个字节[]在另一个字节[]中,最好的搜索方法是什么

比如说

byte[] first = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05 };
byte[] second = new byte[] { 0x01, 0x02 };
byte[] third = new byte[] { 0x01, 0x03 };
该方法将返回:

first.Contains(second); // true
first.Contains(third); // false
second.Contains(third); // false
谢谢

你可以使用Jb,在第一场比赛中提前出场

例如:

静态类ByteArrayRocks
{
公共静态bool包含(此字节[]自身,字节[]候选)
{
if(IsEmptyLocate(自我,候选人))
返回false;
for(int i=0;i(array.Length-位置))
返回false;
for(int i=0;iarray.Length;
}
}
班级计划
{
静态void Main()
{
var data=新字节[]{23,36,43,76,125,56,34,234,12,3,5,76,8,0,6,125,234,56,211,122,22,4,7,89,76,64,12,3,5,76,8,0,6,125};
var模式=新字节[]{12,3,5,76,8,0,6,125,11};
WriteLine(data.Contains(pattern));
Console.ReadKey();
}
}
与某些数组相比,这种方法的效率要低得多,因为如果存在不匹配,它可以更快地跳过数组。在许多其他算法中,有C#实现

这是一个使用它和horspool的wikipedia实现的改编

static class Horspool
{
    private static int[] BuildBadSkipArray(byte[] needle)
    {
        const int MAX_SIZE = 256;

        int[] skip = new int[MAX_SIZE];
        var needleLength = needle.Length;

        for (int c = 0; c < MAX_SIZE; c += 1)
        {
            skip[c] = needleLength;
        }

        var last = needleLength - 1;

        for (int scan = 0; scan < last; scan++)
        {
            skip[needle[scan]] = last - scan;
        }

        return skip;
    }

    public static bool ContainsHorspool(this byte[] haystack, byte[] needle)
    {
        var hlen = haystack.Length;
        var nlen = needle.Length;
        var badCharSkip = BuildBadSkipArray(needle);
        var last = nlen - 1;

        int offset = 0;
        int scan = nlen;

        while (offset + last < hlen)
        {

            for (scan = last; haystack[scan + offset] == needle[scan]; scan = scan - 1)
            {
                if (scan == 0)
                {
                    return true;
                }
            }

            offset += badCharSkip[haystack[scan + offset]];

        }

        return false;
    }
}
静态类
{
私有静态int[]BuildBadSkipArray(字节[])
{
const int MAX_SIZE=256;
int[]跳过=新int[MAX_SIZE];
var针头长度=针头长度;
对于(int c=0;c
您是否在寻求一种比您所展示的更好的方法?我所展示的只是在数组中搜索单个字节。在匹配序列中需要多个字节。注意,这要求任何匹配,而原始要求多个匹配+1表示建议Boyer Moore。任何字符串搜索算法都应该适用于这里。上面的第二个列表对我来说不起作用,结果是输入错误<代码>偏移量+=行应已读取
偏移量+=badCharSkip[haystack[last+offset]]
static class Horspool
{
    private static int[] BuildBadSkipArray(byte[] needle)
    {
        const int MAX_SIZE = 256;

        int[] skip = new int[MAX_SIZE];
        var needleLength = needle.Length;

        for (int c = 0; c < MAX_SIZE; c += 1)
        {
            skip[c] = needleLength;
        }

        var last = needleLength - 1;

        for (int scan = 0; scan < last; scan++)
        {
            skip[needle[scan]] = last - scan;
        }

        return skip;
    }

    public static bool ContainsHorspool(this byte[] haystack, byte[] needle)
    {
        var hlen = haystack.Length;
        var nlen = needle.Length;
        var badCharSkip = BuildBadSkipArray(needle);
        var last = nlen - 1;

        int offset = 0;
        int scan = nlen;

        while (offset + last < hlen)
        {

            for (scan = last; haystack[scan + offset] == needle[scan]; scan = scan - 1)
            {
                if (scan == 0)
                {
                    return true;
                }
            }

            offset += badCharSkip[haystack[scan + offset]];

        }

        return false;
    }
}