【问题标题】:Hex Dump EXE File十六进制转储 EXE 文件
【发布时间】:2017-02-20 02:55:01
【问题描述】:

如何正确显示 EXE 文件 "C:/Path/To/File.exe" 的十六进制形式的内容?到目前为止,我有:

byte[] BytArr = File.ReadAllBytes("C:/Path/To/File.exe")

我尝试使用 switch 语句(此处未显示)读取每隔几个字节并应输出适当的十六进制代码,但它失败了。我该怎么办?如果有人可以帮助我,我将不胜感激。


请注意,答案代码的格式不正确而且效率很低(来源:https://www.codeproject.com/articles/36747/quick-and-dirty-hexdump-of-a-byte-array),但我确实努力正确格式化它。 答案代码:

    using System.Text;

    namespace HexDump  
    {
        class Utils
        {
            public static string HexDump(byte[] bytes, int bytesPerLine = 16)
            {
                if (bytes == null) return "<null>";
                int bytesLength = bytes.Length;

                char[] HexChars = "0123456789ABCDEF".ToCharArray();

                int firstHexColumn =
              8                   // 8 characters for the address
            + 3;                  // 3 spaces

                int firstCharColumn = firstHexColumn
            + bytesPerLine * 3       // - 2 digit for the hexadecimal value and 1 space
            + (bytesPerLine - 1) / 8 // - 1 extra space every 8 characters from the 9th
            + 2;                  // 2 spaces 

                int lineLength = firstCharColumn
            + bytesPerLine           // - characters to show the ascii value
            + Environment.NewLine.Length; // Carriage return and line feed (should normally be 2)

                char[] line = (new String(' ', lineLength - Environment.NewLine.Length) + Environment.NewLine).ToCharArray();
                int expectedLines = (bytesLength + bytesPerLine - 1) / bytesPerLine;
                StringBuilder result = new StringBuilder(expectedLines * lineLength);

                for (int i = 0; i < bytesLength; i += bytesPerLine)
                {
                    line[0] = HexChars[(i >> 28) & 0xF];
                    line[1] = HexChars[(i >> 24) & 0xF];
                    line[2] = HexChars[(i >> 20) & 0xF];
                    line[3] = HexChars[(i >> 16) & 0xF];
                    line[4] = HexChars[(i >> 12) & 0xF];
                    line[5] = HexChars[(i >> 8) & 0xF];
                    line[6] = HexChars[(i >> 4) & 0xF];
                    line[7] = HexChars[(i >> 0) & 0xF];

                    int hexColumn = firstHexColumn;
                    int charColumn = firstCharColumn;

                    for (int j = 0; j < bytesPerLine; j++)
                    {
                        if (j > 0 && (j & 7) == 0) hexColumn++;
                        if (i + j >= bytesLength)
                        {
                            line[hexColumn] = ' ';
                            line[hexColumn + 1] = ' ';
                            line[charColumn] = ' ';
                        }
                        else
                        {
                            byte b = bytes[i + j];
                            line[hexColumn] = HexChars[(b >> 4) & 0xF];
                            line[hexColumn + 1] = HexChars[b & 0xF];
                            line[charColumn] = (b < 32 ? '·' : (char)b);
                        }
                        hexColumn += 3;
                        charColumn++;
                    }
                    result.Append(line);
                }
                return result.ToString();
            }
        }
    }

【问题讨论】:

标签: c# arrays hexdump


【解决方案1】:

这里有一些简单的代码,它将一次(步骤)的字节 4 与空格分隔符(分隔符)合并:

int step = 4;
string delimiter = " ";
for(int i = 0; i < BytArr.Length;i += step)
{
    for(int j = 0; j < step; j++)
    {
        Console.Write(BytArr[i + j].ToString("X2"));
    }
    Console.Write(delimiter);
}

【讨论】:

  • 您的答案非常有用(答案可能以不同的方式有用),所以我投了赞成票。为了防止大家产生混淆,我决定不将“已接受”状态移动到您的答案中(这仍然很有帮助),尽管您的代码的密度和质量都很高。
  • 应该是这个,因为 Stackoverflow 标准认为另一个不好,因为它的内容纯粹是其他地方的链接
  • 在这种情况下,我移动了“接受”状态。我们仍然应该感谢 Tectrendz 所做的努力。
【解决方案2】:

URL 展示了如何在 C 中转储。搜索页面末尾给出的 C 示例。 这个URL 显示了 C# 中的示例

【讨论】:

  • 感谢您的努力,但我指的是 C#,而不是 C(我在其中找不到 C#)。
  • 添加了 C# 示例。
  • 您的回答真的很有帮助。谢谢(赞成并接受)!我将在我的问题中发布代码供人们查看。
猜你喜欢
  • 2016-10-19
  • 2014-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多