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

时间:2017-02-03 23:03:33

标签: xml 32-bit

这是一个文本文件的要点,其中包含打包成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/

1 个答案:

答案 0 :(得分:0)

它有这样的信息..

GAMECTRLTTOWSON#5 Towson MMARYLAND Maryland 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();
}