Click here to Skip to main content
15,891,375 members
Articles / Programming Languages / C#
Tip/Trick

File Data Inspector

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
2 Apr 2012CPOL 11.3K   55   1   3
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)


Written By
Software Developer (Senior)
Italy Italy
Creator of:
Impulse Media Player http://impulsemediaplayer.codeplex.com
Audio Pitch & Shift http://audiops.codeplex.com
Ultimate Music Tagger http://umtagger.codeplex.com
Modern Log Viewer http://modernlogviewer.codeplex.com
Pitch Tuner http://pitchtuner.codeplex.com
Modern Audio Tagger http://modernaudiotagger.codeplex.com
Win Log Inspector http://windowsloganalyzer.com/win-log-inspector/
Win Log Analyzer http://windowsloganalyzer.com/win-log-analyzer/

Comments and Discussions

 
BugSorry, but your code is wrong Pin
Andreas Kroll2-Apr-12 11:50
Andreas Kroll2-Apr-12 11:50 
I always am glad if people take the leap to write an article instead of just consuming it. Sharing with others things that someone found useful or sharing things one had problems with is a big step in the right direction.

BUT:
I always am very sad to see short articles, describing nearly nothing of the problem that urged someone to write a piece of code, the reasons why he/she did not take an already existing tool for that, etc.

Your article has 4 (four!!!) lines of text, 1 image, 2 code-pieces none of which are described at all.

The code you found on the web has no source link, no credit to another author. But in this case it is good, because the code is plainly WRONG, because it does not do what you promise it to do:

Having a text like ABABABCD and searching for ABABCD yields no result because:
Text:        ABABABCD 
Matching:    A
             AB
             ABA
             ABAB
             No further match, so start the search sequence from start but from current 
                position in text
                 A
                 AB
                 No further match, so return -1 at the end of the function. 


To correct the code you would have to remember where you started your current match and return to the position + 1 in case of a failure.

Don't take the criticism personally. Try to improve the article by involving the readers more in your case.
I'd be glad to see the next version of your article.
Today is the tomorrow of yesterday...

GeneralRe: Sorry, but your code is wrong Pin
Manfred Rudolf Bihy2-Apr-12 21:42
professionalManfred Rudolf Bihy2-Apr-12 21:42 
GeneralRe: Sorry, but your code is wrong Pin
Fabrizio Stellato2-Apr-12 21:54
Fabrizio Stellato2-Apr-12 21:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.