如何读取或解释十六进制数据?

How can I read or interpret hexadecimal data?

这是一个文本文件的要点,其中包含打包成 4 个块的位:

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

这是前 20 行:

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

我如何将这个十六进制数据插入到 XML 文件中?

此读数是运动比赛的统计数据。我预计基本上包含五个部分:两组名单、逐场比赛和两组统计数据。知道如何从这些数据中推断出这些信息吗?


更新 // 编辑:

找到这个将十六进制转换为文本的酷站点:

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

里面有这样的信息..

GAMECTRLTTOWSON #5 Towson MMARYLAND 马里兰 2-1

以上评论非常相关。我会重新考虑这种方法。 下面的代码向您展示了数据,但它与我推荐的相反。其中大部分功能实际上都隐藏在 .NET 框架中——我将其编码出来是为了让您考虑除了将十六进制字符串解析为数据之外的真正解决方案。

如果您可以在十六进制文本文件被写出之前访问数据,您的状态会更好。

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();
}