Sunday, 15 January 2012

How to locate subarray from an array c# -



How to locate subarray from an array c# -

i have next byte arrays. byte[] subarray = { 0x00, 0x01, 0x00, 0x01 }; byte[] array = { 0x1a, 0x65, 0x3e, 0x00, 0x01, 0x00, 0x01, 0x2b, 0x4c, 0xaa };

i identify subarray , load preceding bytes byte array i.e. result should below. byte[] result = {0x2b, 0x4c, 0xaa }

i want load after subarray.

any help appreciated.

thanks.

adapted other answer:

byte[] subarray = { 0x00, 0x01, 0x00, 0x01 }; byte[] array = { 0x1a, 0x65, 0x3e, 0x00, 0x01, 0x00, 0x01, 0x2b, 0x4c, 0xaa }; var matchindexes = index in enumerable.range(0, array.length - subarray.length + 1) array.skip(index).take(subarray.length).sequenceequal(subarray) select (int?)index; var matchindex = matchindexes.firstordefault(); if (matchindex != null) { byte[] successor = array.skip(matchindex.value + subarray.length).toarray(); // handle successor here }

c#

No comments:

Post a Comment