十六进制转储EXE文件

时间:2017-02-20 02:55:02

标签: c# arrays hexdump

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

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

我尝试使用开关语句(此处未显示)读取每隔几个字节并输出相应的十六进制代码,但失败了。我该怎么办?如果有人能帮助我,我将非常感激。

请注意答案代码格式不正确并且效率很低(来源: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();
            }
        }
    }

2 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分:-1)

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

中的示例
相关问题