Add byte[].HexDump() extension method

dev_timestamp
Harald Wolff 2019-04-30 11:02:33 +02:00
parent 0a1dbc58d9
commit 2a72c73d27
1 changed files with 38 additions and 0 deletions

View File

@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Text;
namespace ln.types
{
public static class Extensions
@ -135,5 +136,42 @@ namespace ln.types
Array.Copy(me, offset, slice, 0, length);
return slice;
}
public static string HexDump(this byte[] bytes)
{
StringBuilder stringBuilder = new StringBuilder();
for (int n = 0; n < bytes.Length; n += 16)
{
int len = bytes.Length - n;
if (len > 16)
len = 16;
byte[] section = bytes.Slice(n, len);
for (int i=0;i<16;i++)
{
if (i < section.Length)
stringBuilder.AppendFormat("{0:X2} ", section[i]);
else
stringBuilder.Append(" ");
}
stringBuilder.Append(" ");
foreach (byte b in section)
{
if (b < 32)
stringBuilder.Append('.');
else
stringBuilder.Append((char)b);
}
stringBuilder.AppendLine();
}
return stringBuilder.ToString();
}
}
}