2017-02-20 124 views
1

如何正確顯示EXE文件的內容「C:/Path/To/File.exe」以十六進制格式?到目前爲止,我有:十六進制轉儲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 = "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(); 
      } 
     } 
    } 
+0

你們是不是要轉儲文件中的特定格式? – Tectrendz

+0

最好是一個字符串,但對我來說並不重要。謝謝。 – ComputersAreCool

+0

是否有幫助? – Tectrendz

回答

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); 
} 
+0

你的回答非常有用(答案可以用不同的方式有用),所以我投了票。爲了避免混淆所有人,我決定不將「接受」狀態轉移到您的答案中(儘管您的代碼密度和質量都很高,但仍然非常有用)。 – ComputersAreCool

+0

它應該是這一個,因爲它的內容純粹是鏈接其他地方 –

+0

在這種情況下,我移動了「接受」的狀態,因爲另一個不被Stackoverflow標準認爲是好的。我們仍然應該讚揚Tectrendz的努力。 – ComputersAreCool

-1

URL展示瞭如何在C搜索轉儲其朝向頁的最後給定的C樣品。 這URL顯示C#示例

+0

感謝您的努力,但我的意思是C#,而不是C(我在那裏找不到C#)。 – ComputersAreCool

+0

添加了C#示例。 – Tectrendz

+0

你的回答非常有幫助。謝謝(提升並接受)!我會在我的問題中發佈代碼供人們查看。 – ComputersAreCool