Hi,
The code I have posted searches for a pattern of bytes starting from the end
of
Byte[] array *backwords*, if a match is found, return the starting index of
those
found bytes. Since one of the parameters is starting index you can start the
search
anywhere within the Byte[] you are searching.
Example:
Suppose you have a Byte[] that contains the 4 bytes 0x01, 0x01, 0xBA, 0xC8.
Find the starting index for the two bytes 0x01, 0xBA, searching from the
end.
The method would return 0x01, because that is the starting index of the
sequence
of bytes 0x01, 0xBA.
The code I have posted below works, but can you simplfy / optimize the
FindBackward() method?
Thank you.
Russell Mangel
Las Vegas, NV
PS
The Byte[] array in production, will have average size of 3MB - 10MB, they
are .mp3 files.
// Start code
using System;
class Program
{
private Byte[] m_Bytes = { 0x01, 0x01, 0xBA, 0xC8 };
private const UInt32 NOT_FOUND = 0xFFFFFFFF;
private readonly UInt32 END_OF_FILE;
public Program( )
{
END_OF_FILE = (UInt32)m_Bytes .Length - 1;
}
static void Main( string[] args )
{
Program p = new Program( );
Byte[] searchFor = { 0x01, 0xBA };
UInt32 startSearchInde x = 0x03;
UInt32 x = p.FindBackward( searchFor, startSearchInde x );
Console.WriteLi ne( "{0:X8}", x );
Console.ReadLin e( );
}
public UInt32 FindBackward( Byte[] bytes, UInt32 index )
{
if (bytes == null || index END_OF_FILE )
{
return NOT_FOUND;
}
Boolean isMatch = true;
UInt32 i = index;
UInt32 size = ( UInt32 )bytes.Length - 1;
UInt32 j = size;
UInt32 t = 0;
do
{
isMatch = true;
j = size;
t = 0;
do
{
if ( m_Bytes[ i - t ] != bytes[ j ] )
{
isMatch = false;
break;
}
t++;
}
while ( j-- 0 );
if ( isMatch )
{
return ( i - size ); // Success
}
}
while ( i-- 0 );
return NOT_FOUND;
}
}
// End Code
The code I have posted searches for a pattern of bytes starting from the end
of
Byte[] array *backwords*, if a match is found, return the starting index of
those
found bytes. Since one of the parameters is starting index you can start the
search
anywhere within the Byte[] you are searching.
Example:
Suppose you have a Byte[] that contains the 4 bytes 0x01, 0x01, 0xBA, 0xC8.
Find the starting index for the two bytes 0x01, 0xBA, searching from the
end.
The method would return 0x01, because that is the starting index of the
sequence
of bytes 0x01, 0xBA.
The code I have posted below works, but can you simplfy / optimize the
FindBackward() method?
Thank you.
Russell Mangel
Las Vegas, NV
PS
The Byte[] array in production, will have average size of 3MB - 10MB, they
are .mp3 files.
// Start code
using System;
class Program
{
private Byte[] m_Bytes = { 0x01, 0x01, 0xBA, 0xC8 };
private const UInt32 NOT_FOUND = 0xFFFFFFFF;
private readonly UInt32 END_OF_FILE;
public Program( )
{
END_OF_FILE = (UInt32)m_Bytes .Length - 1;
}
static void Main( string[] args )
{
Program p = new Program( );
Byte[] searchFor = { 0x01, 0xBA };
UInt32 startSearchInde x = 0x03;
UInt32 x = p.FindBackward( searchFor, startSearchInde x );
Console.WriteLi ne( "{0:X8}", x );
Console.ReadLin e( );
}
public UInt32 FindBackward( Byte[] bytes, UInt32 index )
{
if (bytes == null || index END_OF_FILE )
{
return NOT_FOUND;
}
Boolean isMatch = true;
UInt32 i = index;
UInt32 size = ( UInt32 )bytes.Length - 1;
UInt32 j = size;
UInt32 t = 0;
do
{
isMatch = true;
j = size;
t = 0;
do
{
if ( m_Bytes[ i - t ] != bytes[ j ] )
{
isMatch = false;
break;
}
t++;
}
while ( j-- 0 );
if ( isMatch )
{
return ( i - size ); // Success
}
}
while ( i-- 0 );
return NOT_FOUND;
}
}
// End Code
Comment