Created
April 28, 2013 03:41
-
-
Save shilrobot/5475784 to your computer and use it in GitHub Desktop.
C# hex dumper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Text; | |
namespace Whatever | |
{ | |
public static class HexDumper | |
{ | |
public static string HexDump(byte[] data) | |
{ | |
var sb = new StringBuilder(); | |
for (int i = 0; i < data.Length; i += 16) | |
{ | |
sb.AppendFormat("{0:X8}:", i); | |
for (int j = i; j < i + 16; ++j) | |
{ | |
if (j%2 == 0) | |
sb.Append(" "); | |
if (j < data.Length) | |
sb.AppendFormat("{0:X2}", data[j]); | |
else | |
sb.Append(" "); | |
} | |
sb.Append(" "); | |
for (int j = i; j < i + 16; ++j) | |
{ | |
if (j < data.Length) | |
{ | |
char c = (char)data[j]; | |
if (c >= ' ' && c <= '~') | |
sb.Append(c); | |
else | |
sb.Append("."); | |
} | |
else | |
sb.Append(" "); | |
} | |
sb.AppendLine(); | |
} | |
return sb.ToString(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment