I am trying to find ways to get the hex data from a file opened in a windows form using the open file dialog. I've done some reading and have found that .NET Framework used to have a byte viewer component built into System.Design that was a quick standard way to view the data needed; however upon trying this (ignorantly forgetting that most information found in the first page of Google's search engine for things like this are outdated 50% of the time) I found that System.Design.ByteViewer did not exist even after adding System.Design as a reference. I am prepared to write my own component to handle this in the long run anyways since I do not want it displayed in the traditional sense of a hex editor where the data is displayed via offsets.

In a standard hex editor we all know that data is displayed as:
00000000 12 34 56 78 12 34 56 78 12 34 56 78 12 34 56 78

I want mine to be more of a simpler approach to clean up my editor with emphasis on space and organization. Mine will have the downside of a longer list of data (in appearence) but will optimize work space:

00000000 12345678
00000004 12345678
00000008 12345678
0000000c 12345678

I like this view better because it allows me to stick with the overall idea of the application in a simpler form and still manage to get my end task done.

Has anyone else worked on this type of project and if so can you give me more up to date areas to use? I'm not looking for complete code snippets, but at least a list of references and the classes I can use within them to accomplish my task. Also an idea on what control to use to represent my data would be nice too.

Thanks in advance!

The following has been tested with a text file.

public enum OutputGrouping : int
{
    Group1AsciiCodeNewLineSeparated,   //12       - 1 ASCII code per line
    Group1AsciiCodeSpaceSeparated,     //12       - 1 ASCII code, then a space
    Group2AsciiCodesNewLineSeparated,  //1234     - group of 2 ASCII codes per line
    Group4AsciiCodesNewLineSeparated,  //12345678 - group of 4 ASCII codes per line
}//OutputGrouping

ConvertTextFileToASCIICodes:

public static string ConvertTextFileToASCIICodes(string inputFilename, OutputGrouping grouping)
{
    StringBuilder outputSb = new StringBuilder();
    string tempStr = String.Empty;
    string prependStr = String.Empty;
    int position = 0;

    //read file data
    byte[] fileDataArr = System.IO.File.ReadAllBytes(inputFilename);

    for (int i=0; i < fileDataArr.Length; i++)
    {
        //convert to 2-char hex string
        convertedByteStrArr[i] = fileDataArr[i].ToString("X2");

        //group ASCII codes
        switch (grouping)
        {
            case OutputGrouping.Group1AsciiCodeSpaceSeparated:
                //separate ASCII codes by space
                outputSb.Append(convertedByteStrArr[i].ToString() + " ");
                break;

            case OutputGrouping.Group1AsciiCodeNewLineSeparated:
                //separate ASCII codes by NewLine
                outputSb.AppendLine(convertedByteStrArr[i].ToString());
                break;

            case OutputGrouping.Group2AsciiCodesNewLineSeparated:
                //two ASCII codes together followed by a NewLine
                if ((i+1) % 2 == 0)
                {
                    //convert position to 8-char hex string
                    prependStr = position.ToString("X8");

                    //append current ASCII code
                    tempStr += convertedByteStrArr[i].ToString();

                    //prepend with position as 8-char hex string
                    outputSb.AppendLine(prependStr + " " + tempStr);

                    Console.WriteLine("tempStr: " + prependStr + " " + tempStr);

                    //re-initialize
                    tempStr = string.Empty;

                    //set position
                    position = i + 1;
                }//if
                else
                {
                    //append current ASCII code
                    tempStr += convertedByteStrArr[i].ToString();
                }//else

                break;

            case OutputGrouping.Group4AsciiCodesNewLineSeparated:
                //four ASCII codes together followed by a NewLine
                if ((i+1) % 4 == 0)
                {
                    //convert position to 8-char hex string
                    prependStr = position.ToString("X8");

                    //append current ASCII code
                    tempStr += convertedByteStrArr[i].ToString();

                    //prepend with position as 8-char hex string
                    outputSb.AppendLine(prependStr + " " + tempStr);

                    //re-initialize
                    tempStr = string.Empty;

                    //set position
                    position = i + 1;
                }//if
                else
                {
                    //append current ASCII code
                    tempStr += convertedByteStrArr[i].ToString();
                }//else

                break;
        }//switch
    }//for


    //add any remaining ASCII codes
    if (tempStr != String.Empty)
    {
        //convert position to 8-char hex string
        prependStr = position.ToString("X8");

        //prepend with position as 8-char hex string
        outputSb.AppendLine(prependStr + " " + tempStr);
    }//if

    return outputSb.ToString();
}//ConvertTextFileToASCIICodes

Usage:

ConvertTextFileToASCIICodes(@"C:\Temp\Test.txt", OutputGrouping.Group4AsciiCodesNewLineSeparated);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.