Vince,
If you have the contents in a string form, then you can use the IndexOf
method. However, if you need to search by bytes (and you are using
characters here, indicating to me that these are ascii values), then you
could create a generic method, like so:
public static int ByteIndexOf(byte[] searched, byte[] find, int start)
{
// Do standard error checking here.
// Did the values match?
bool matched = false;
// Cycle through each byte of the searched. Do not search past
// searched.Length - find.Length bytes, since it's impossible
// for the value to be found at that point.
for (int index = start; index <= searched.Length - find.Length; ++index)
{
// Assume the values matched.
matched = true;
// Search in the values to be found.
for (int subIndex = 0; subIndex < find.Length; ++subIndex)
{
// Check the value in the searched array vs the value
// in the find array.
if (find[subIndex] != searched[index + subIndex])
{
// The values did not match.
matched = false;
// Break out of the loop.
break;
}
}
// If the values matched, return the index.
if (matched)
{
// Return the index.
return index;
}
}
// None of the values matched, return -1.
return -1;
}
Of course, this could probably be optimized, but this is the general
idea.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
-
mv*@spam.guard.caspershouse.com
"Vince Panuccio" <to*********************@hotmail.comwrote in message
news:11********************@p77g2000hsh.googlegrou ps.com...
Hello,
If im looking for a pattern of byes in a byte array, what would be the
best approach?
I could convert the array into a string and use IndexOf recursivly by
remembering the last position I encountered a match, but it seems to
be the wrong way to go about it, especially if I have an array a
couple of MB in size.
My initial thoughts are to just use a for loop and check for a byte
matching the first instance of the search pattern.
For example
asodaiudoqiiqmngm,3n23n4234kj23rasdrwebHelloWorldk ajsdiouahsd
My way of doing this would be to look for 'H' and then check every
character after that using a bunch of If's.
Is there a built in method or an algorithm I could use? Google has not
helped.
Cheers