Skip to content

Instantly share code, notes, and snippets.

@blinds52
Last active April 14, 2020 01:02
Show Gist options
  • Save blinds52/a3f3d0a907d9c5006a5e13991a14cd2d to your computer and use it in GitHub Desktop.
Save blinds52/a3f3d0a907d9c5006a5e13991a14cd2d to your computer and use it in GitHub Desktop.
HexDump (binary visualizer)
00000000 00 00 04 D4 00 00 00 40 23 46 42 20 23 43 4F 4C ···O···@#FB #COL
00000010 43 30 32 34 23 46 4D 54 52 41 57 20 23 4A 50 47 C024#FMTRAW #JPG
00000020 64 30 39 30 23 54 48 52 64 31 32 38 23 47 4D 4D d090#THRd128#GMM
00000030 55 47 31 38 23 47 4D 54 52 45 44 20 68 31 30 30 UG18#GMTRED h100
00000040 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F ················
00000050 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F ················
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
namespace Parser
{
public static class HumanizerExtensions
{
public static string HumanizeBinary(this ArraySegment<byte> data)
{
return Encoding.ASCII.GetString(
data
.Select(f => (f < 32 || f >= 127) ? (byte)46 : f)
.ToArray());
}
public static string HumanizeAsHex(this ArraySegment<byte> data, string separator = " ")
{
return string.Join(separator, data.Select(f => f.ToString("X2", CultureInfo.InvariantCulture)).ToArray());
}
public static string HexDump(this ArraySegment<byte> bytes, int intendChars = 2, int bytesPerLine = 16)
{
if (bytes.Count == 0) return "<null>";
var bytesLength = bytes.Count;
var hexChars = "0123456789ABCDEF".ToCharArray();
var firstHexColumn =
intendChars
+ 8 // 8 characters for the address
+ 3; // 3 spaces
var firstCharColumn = firstHexColumn
+ (bytesPerLine * 3) // 2 digit for the hexadecimal value and 1 space
+ ((bytesPerLine - 1) / 8) // 1 extra space every 8 characters from the 9th
+ 2; // 2 spaces
var lineLength = firstCharColumn
+ bytesPerLine // characters to show the ascii value
+ Environment.NewLine.Length; // Carriage return and line feed (should normally be 2)
var line = (new string(' ', lineLength - Environment.NewLine.Length) + Environment.NewLine).ToCharArray();
var expectedLines = (bytesLength + bytesPerLine - 1) / bytesPerLine;
var result = new StringBuilder(Math.Min(expectedLines, 12) * lineLength);
var addedLines = 0;
for (var i = 0; i < bytesLength; i += bytesPerLine)
{
addedLines++;
if (bytes.Count > 2048 && addedLines > 8 && addedLines < expectedLines - 2)
{
if (addedLines != 9) continue;
result.Append(new string(' ', intendChars));
result.AppendLine("...");
}
else
{
line[0] = hexChars[(i >> 28) & 0xF];
line[1] = hexChars[(i >> 24) & 0xF];
line[2] = hexChars[(i >> 20) & 0xF];
line[3] = hexChars[(i >> 16) & 0xF];
line[4] = hexChars[(i >> 12) & 0xF];
line[5] = hexChars[(i >> 8) & 0xF];
line[6] = hexChars[(i >> 4) & 0xF];
line[7] = hexChars[(i >> 0) & 0xF];
var hexColumn = firstHexColumn;
var charColumn = firstCharColumn;
for (var j = 0; j < bytesPerLine; j++)
{
if (j > 0 && (j & 7) == 0) hexColumn++;
if (i + j >= bytesLength)
{
line[hexColumn] = ' ';
line[hexColumn + 1] = ' ';
line[charColumn] = ' ';
}
else
{
var b = bytes[i + j];
line[hexColumn] = hexChars[(b >> 4) & 0xF];
line[hexColumn + 1] = hexChars[b & 0xF];
line[charColumn] = (b < 32 ? '·' : (char) b);
}
hexColumn += 3;
charColumn++;
}
result.Append(new string(' ', intendChars));
result.Append(line);
}
}
return result.ToString();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment