Last active
December 23, 2017 04:53
-
-
Save mkropat/8f1874359946cce0d12d963d854ec502 to your computer and use it in GitHub Desktop.
This file contains 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
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