title: Hex Dump EXE File
body: <p>How do I properly display the contents of an EXE file <strong>"C:/Path/To/File.exe"</strong> in hexadecimal form? So far, I have:</p>
  
  <pre><code>byte[] BytArr = File.ReadAllBytes("C:/Path/To/File.exe")
  </code></pre>
  
  <p>I tried using a <strong>switch</strong> statement (not shown here) that reads every few bytes and should output the appropriate hexadecimal code, but it failed. What should I do? I would really appreciate it if anyone can help me.</p>
  
  <hr>
  
  <p>Beware that the answer code isn't well formatted and is rather inefficient (source: <a href="https://www.codeproject.com/articles/36747/quick-and-dirty-hexdump-of-a-byte-array" rel="nofollow noreferrer">https://www.codeproject.com/articles/36747/quick-and-dirty-hexdump-of-a-byte-array</a>), but I did make an effort to format it properly.
  <strong>Answer Code:</strong></p>
  
  <pre><code>    using System.Text;
  
      namespace HexDump  
      {
          class Utils
          {
              public static string HexDump(byte[] bytes, int bytesPerLine = 16)
              {
                  if (bytes == null) return "&lt;null&gt;";
                  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 &lt; bytesLength; i += bytesPerLine)
                  {
                      line[0] = HexChars[(i &gt;&gt; 28) &amp; 0xF];
                      line[1] = HexChars[(i &gt;&gt; 24) &amp; 0xF];
                      line[2] = HexChars[(i &gt;&gt; 20) &amp; 0xF];
                      line[3] = HexChars[(i &gt;&gt; 16) &amp; 0xF];
                      line[4] = HexChars[(i &gt;&gt; 12) &amp; 0xF];
                      line[5] = HexChars[(i &gt;&gt; 8) &amp; 0xF];
                      line[6] = HexChars[(i &gt;&gt; 4) &amp; 0xF];
                      line[7] = HexChars[(i &gt;&gt; 0) &amp; 0xF];
  
                      int hexColumn = firstHexColumn;
                      int charColumn = firstCharColumn;
  
                      for (int j = 0; j &lt; bytesPerLine; j++)
                      {
                          if (j &gt; 0 &amp;&amp; (j &amp; 7) == 0) hexColumn++;
                          if (i + j &gt;= bytesLength)
                          {
                              line[hexColumn] = ' ';
                              line[hexColumn + 1] = ' ';
                              line[charColumn] = ' ';
                          }
                          else
                          {
                              byte b = bytes[i + j];
                              line[hexColumn] = HexChars[(b &gt;&gt; 4) &amp; 0xF];
                              line[hexColumn + 1] = HexChars[b &amp; 0xF];
                              line[charColumn] = (b &lt; 32 ? '·' : (char)b);
                          }
                          hexColumn += 3;
                          charColumn++;
                      }
                      result.Append(line);
                  }
                  return result.ToString();
              }
          }
      }
  </code></pre>