Wie kann ich den Inhalt einer EXE-Datei "C: /Path/To/File.exe" in hexadezimaler Form richtig anzeigen? Bisher habe ich:Hex Dump EXE-Datei
byte[] BytArr = File.ReadAllBytes("C:/Path/To/File.exe")
Ich habe versucht, einen Schalter Anweisung (hier nicht dargestellt), die alle paar Bytes liest und ausgeben sollte die entsprechende Hexadezimal-Code, aber es ist fehlgeschlagen. Was soll ich machen? Ich würde es wirklich schätzen, wenn mir jemand helfen kann.
Beachten Sie, dass die Antwort-Code nicht gut formatiert und ist ziemlich ineffizient (Quelle: https://www.codeproject.com/articles/36747/quick-and-dirty-hexdump-of-a-byte-array), aber ich habe Mühe machen es richtig zu formatieren. Antwort Code:
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 = "ABCDEF".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();
}
}
}
Versuchen Sie, Datei in einem bestimmten dump Format? – Tectrendz
Vorzugsweise eine Saite, aber das macht mir nichts aus. Vielen Dank. – ComputersAreCool
Hilft das Beispiel? – Tectrendz