Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

File Data Inspector

5.00/5 (1 vote)
2 Apr 2012CPOL 11.7K   55  
Simply search data in file

Introduction

This is just a simple utility to search data in a file.

It may be useful to inspect data inside a file.

Image 1

Using the Code

This piece of code I found around on the web shows how to find an array of bytes inside another one:

C#
public static int IndexOfBytes(byte[] array, byte[] pattern, int startIndex, int count)
{
    int i = startIndex;
    int endIndex = count > 0 ? startIndex + count : array.Length;
    int fidx = 0;

    while (i < endIndex)
    {
        fidx = (array[i] == pattern[fidx]) ? ++fidx : 0;
        if (fidx == pattern.Length)
        {
            return i - fidx + 1;
        }
        i++;
    }
    return -1;
} 

This is the number conversion in the bytes routine, according to the format selected (8, 16, 32, or 64 bit):

C#
switch (numberBase)
{
    case 8:
        byte2Find = new byte[] { byte.Parse(textBoxNumber.Text) };
        break;
    case 16:
        byte2Find = BitConverter.GetBytes(Convert.ToInt16(textBoxNumber.Text));
        break;
    case 32:
        byte2Find = BitConverter.GetBytes(Convert.ToInt32(textBoxNumber.Text));
        break;
    case 64:
        byte2Find = BitConverter.GetBytes(Convert.ToInt64(textBoxNumber.Text));
        break;
    default:
        byte2Find = new byte[0];
        break;
}   

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)