Search code examples
xml32-bit

How can I read or interpret hexadecimal data?


Here's a gist of a text file containing bits packed into blocks of 4:

https://gist.github.com/ukudala/f532809ce7de4f5599bad5c3b61eae9a

And here are the first 20 lines:

6f5e 0000 4c18 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 5645 5253 494f 4e20 3152 554c 4543 5452 4c04 000f 0000 0000 0000 0000 0005 001e 003c 005a 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0047 414d 4543 5452 4c54 544f 5753 4f4e 2020 0023 3520 546f 7773 6f6e 2020 2020 2020 2020 2020 2000 4d4d 4152 594c 414e 4400 4d61 7279 6c61 6e64 2020 2020 2020 2020 2020 2020 0032 2d31 2020 2020 2020 2020 2020 2020 2020 2020 2000 332d 3120 2020 2020 2020 2020 2020 2020 2020 2020 0032 2d31 2020 2020 2020 2020 2020 2020 2020 2020 2000 332d 3120 2020 2020 2020 2020 2020 2020 2020 2020 0005 0019 0000 0000 000b 0000 0000 0004 0000 0000 0004 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000

How would I interpolate this hexadecimal data into, for instance, an XML file?

This readout is the statistics from an athletic contest. I anticipate there to be essentially five sections contained: two sets of rosters, a play by play of the game, and two sets of statistics. Any idea how I can extrapolate this information from this data?


Update // Edit:

Found this cool site that converts hexadecimal to text:

http://www.unit-conversion.info/texttools/hexadecimal/


Solution

  • It has information like this in it ..

    GAMECTRLTTOWSON #5 Towson MMARYLAND Maryland 2-1

    The comments above are extremely relevant. I would rethink the approach. The following code shows you the data but it is the opposite of what I would recommend. Much of this functionality is actually buried in the .NET framework – I coded it out to get you thinking about a real solution besides parsing a hex string as data.

    If you had access to the data just before that hex as text file was written out you would be in better shape.

    public void ExampleTest1()
    {
    
        string hexString = "6f5e 0000 4c18 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 5645 5253 494f 4e20 3152 554c 4543 5452 4c04 000f 0000 0000 0000 0000 0005 001e 003c 005a 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0047 414d 4543 5452 4c54 544f 5753 4f4e 2020 0023 3520 546f 7773 6f6e 2020 2020 2020 2020 2020 2000 4d4d 4152 594c 414e 4400 4d61 7279 6c61 6e64 2020 2020 2020 2020 2020 2020 0032 2d31 2020 2020 2020 2020 2020 2020 2020 2020 2000 332d 3120 2020 2020 2020 2020 2020 2020 2020 2020 0032 2d31 2020 2020 2020 2020 2020 2020 2020 2020 2000 332d 3120 2020 2020 2020 2020 2020 2020 2020 2020 0005 0019 0000 0000 000b 0000 0000 0004 0000 0000 0004 0000 0000";
    
        string strFromHexStr = FxByteArrayFromHexString(hexString);
    
        System.Diagnostics.Debug.WriteLine(strFromHexStr);
    
        // something like ...
        // GAMECTRLTTOWSON   #5 Towson            MMARYLAND Maryland             2-1
    
    }
    
    
    string FxByteArrayFromHexString(string HexLikeStringIn)
    {
        string retFianl = string.Empty;
        StringBuilder sbWork = new StringBuilder(string.Empty);
        char[] chrArrayFromHexString = null;
        char chrA = ' ';
        char chrB = ' ';
        bool readFront = true;
        byte curByt = 0;
        string tmpStr = string.Empty;
        bool hexChar = false;
        Int32 twoDigitHexAsNumber = 0;
    
        try
        {
            chrArrayFromHexString = HexLikeStringIn.ToCharArray();
    
            foreach(char curChr in chrArrayFromHexString)
            {
                // accumlate in chunks of two 
    
                hexChar = false;            
                twoDigitHexAsNumber = (-1);
    
                curByt = Convert.ToByte(curChr);
    
                if ((curByt >= 48) && (curByt <= 57)) // between "0" and "9" as strings 
                {
                    hexChar = true;
                }
                else if ((curByt >= 97) && (curByt <= 102)) // hex letters small "a" to "f" 
                {
                    hexChar = true;
                }
                else if ((curByt >= 65) && (curByt <= 70)) // hex letters upper "A" to "F"
                {
                    hexChar = true;
                }
                else
                {
                    // not a hex letter                         
                }
    
                if (hexChar)
                {
                    if (readFront)
                    {
                        readFront = false;
                        chrA = curChr;
                    }
                    else
                    {
                        readFront = true;
                        chrB = curChr;
    
                        if ((chrA != ' ') && (chrB != ' '))
                        {
                            try
                            {
                                twoDigitHexAsNumber = Int32.Parse(chrA.ToString() + chrB.ToString(), System.Globalization.NumberStyles.HexNumber);
                            }
                            catch (Exception exHxParse)
                            {
    
                            }
                        }
    
                        // twoDigitHexAsNumber might have our parse - start over on the two digit accumulation 
                        readFront = true;
                        chrA = ' ';
                        chrB = ' ';
                    }
                }
                else
                {
                    //reset both charcters we are tracking 
                    readFront = true;
                    chrA = ' ';
                    chrB = ' ';
                }
    
    
                if (twoDigitHexAsNumber >= 0)
                {
                    //read in a two digit hex number and converted it to decimal 
                    //get the ascii value for this decimal as a character
    
                    if ((twoDigitHexAsNumber >= 32) && (twoDigitHexAsNumber <= 126))
                    {
                        //normal character 
                        sbWork.Append(Convert.ToChar(twoDigitHexAsNumber));
                    }
                    else
                    {
                        // crlf or other relavent control characters 
                        sbWork.Append(' ');
                    }
                }
    
            } //for each loop 
    
            // sbWork now has a strign representation
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(ex.ToString());
        }
    
        return sbWork.ToString();
    }