Skip to content

Instantly share code, notes, and snippets.

@mkropat
Last active December 23, 2017 04:53
Show Gist options
  • Save mkropat/8f1874359946cce0d12d963d854ec502 to your computer and use it in GitHub Desktop.
Save mkropat/8f1874359946cce0d12d963d854ec502 to your computer and use it in GitHub Desktop.
public static string HexDump(byte[] bytes)
{
using (var s = new MemoryStream(bytes))
return HexDump(s);
}
public static string HexDump(Stream s)
{
var result = new StringBuilder();
var buf = new byte[16];
var actual = s.Read(buf, 0, buf.Length);
while (actual > 0)
{
var hex1 = BitConverter.ToString(buf, 0, Math.Min(8, actual)).Replace("-", " ");
var hex2 = BitConverter.ToString(buf, 8, Math.Max(actual - 8, 0)).Replace("-", " ");
var padding = new String(' ', (buf.Length-actual)*3);
var ascii = Regex.Replace(Encoding.Default.GetString(buf, 0, actual), @"\p{Cc}", "·");
result.AppendLine($"{s.Position - actual:X8} {hex1} {hex2}{padding} {ascii}");
actual = s.Read(buf, 0, buf.Length);
}
return result.ToString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment