Skip to content

Instantly share code, notes, and snippets.

@shilrobot
Created April 28, 2013 03:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shilrobot/5475784 to your computer and use it in GitHub Desktop.
Save shilrobot/5475784 to your computer and use it in GitHub Desktop.
C# hex dumper
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