Skip to content

Instantly share code, notes, and snippets.

@TETYYS
Created April 2, 2015 08:02
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 TETYYS/6354c53bf5d634b89aad to your computer and use it in GitHub Desktop.
Save TETYYS/6354c53bf5d634b89aad to your computer and use it in GitHub Desktop.
Dumps bytes in cool hex fashion to console
/// <summary>
/// Dumps bytes in cool hex fashion to console
/// </summary>
/// <param name="In">Bytes</param>
/// <param name="Width">Width of table</param>
/// <param name="Char">Char for non-printable characters</param>
/// <returns></returns>
public static string PrintBytes(byte[] In, int Width = 8, char Char = '.') {
var sb = new StringBuilder();
int size = In.Length;
sb.AppendFormat("Dump size: {0}\n", size);
for (int x = 0; x < Math.Floor((double)size / Width); x++) {
for (int i = 0; i < Width; i++) {
sb.AppendFormat("{0:X2} ", In[(x*Width)+i]);
}
sb.Append("\t");
for (int i = 0; i < Width; i++) {
if (In[(x*Width)+i] < 0x20 || In[(x*Width)+i] > 0x7E)
sb.Append(Char);
else
sb.Append((char)In[(x*Width)+i]);
}
sb.AppendLine();
}
if (size % Width != 0) {
for (var x = (int)Math.Floor((double)size / Width); x < (Math.Floor((double)size / Width) + size % Width); x++) {
sb.AppendFormat("{0:X2} ", In[x]);
}
for (int x = 0; x < Width - (size % Width); x++) {
sb.Append(" ");
}
sb.Append("\t");
for (var x = (int)Math.Floor((double)size / Width); x < (Math.Floor((double)size / Width) + size % Width); x++) {
if (In[x] < 0x20 || In[x] > 0x7E)
sb.Append(Char);
else
sb.Append((char)In[x]);
}
}
sb.AppendLine();
return sb.ToString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment