M
M
martensit2018-08-14 11:07:00
Arrays
martensit, 2018-08-14 11:07:00

Is there a handy function in c# to find a byte array inside another byte array?

for (int i = 0; i < fs.Length; i++)
    {
    if (fs[i] == 64)
                {
                    if (fs[i + 1] == 115)
                    {
                        if (fs[i + 2] == 46)
                        {
                            if (fs[i + 3] == 119)
                            {
                                if (fs[i + 4] == 104)
                                {
                                    if (fs[i + 5] == 97)
                                    {
                                        if (fs[i + 6] == 116)
                                        {
                                            if (fs[i + 7] == 115)
                                            {
                                                if (fs[i + 8] == 97)
                                                {
                                                    byte[] noom = { fs[i - 11], fs[i - 10], fs[i - 9], fs[i - 8], fs[i - 7], fs[i - 6], fs[i - 5], fs[i - 4], fs[i - 3], fs[i - 2], fs[i - 1] };
                                                    string str = Encoding.UTF8.GetString(noom, 0, noom.Length);
                                                    if (Convert.ToInt64(str) > 0)
                                                    {
                                                        numbers.Add(str);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

Now I'm using something like this. uncomfortable

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
Tom Nolane, 2018-08-14
@martensit

??

static int SearchBytes( byte[] haystack, byte[] needle ) {
    var len = needle.Length;
    var limit = haystack.Length - len;
    for( var i = 0;  i <= limit;  i++ ) {
        var k = 0;
        for( ;  k < len;  k++ ) {
            if( needle[k] != haystack[i+k] ) break;
        }
        if( k == len ) return i;
    }
    return -1;
}

K
Konstantin, 2018-08-14
@nicebmw9

public bool IsContains(this byte[] arr1, byte[] arr2)
        {
            if (arr1.Intersect(arr2).Count() != 0)
                return true;
            return false;
        }

The most elegant solution, no?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question