Hexadecimal output formatting option
It will be very useful, if output pane can be easily switched between decimal and hex notations (when testing ciphering or protocol algorithms, decimal notation is pretty useless). And how about Dump() extension optional formatting specifiers?
-
Nelson Ferrari commented
I saw a HexDump extension somewhere (probably in the forums). Since I cannot remember the place, here is his suggestion (just copy it to "MyExtensions"):
[code]
void Main()
{
// Write code to test your extensions here. Press F5 to compile and run.
byte[] text = Encoding.UTF8.GetBytes("The quick brown fox jumps over the lazy dog");
var textList = new List<byte>(text);
//HexDump(text);
text.HexDump();
textList.HexDump();
}public static class MyExtensions
{
// Write custom extension methods here. They will be available to all queries.
public static object HexDump(this byte[] data)
{
return data
.Select((b, i) => new { Byte = b, Index = i })
.GroupBy(o => o.Index / 16)
.Select(g =>
g
.Aggregate(
new { Hex = new StringBuilder(), Chars = new StringBuilder() },
(a, o) => {a.Hex.AppendFormat("{0:X2} ", o.Byte); a.Chars.Append(Convert.ToChar(o.Byte)); return a;},
a => new { Hex = a.Hex.ToString(), Chars = a.Chars.ToString() }
)
)
.ToList()
.Dump();
}
public static object HexDump(this List<byte> data)
{
return data.ToArray().HexDump();
}
}
[/code] -
Christoph Pock commented
You can write your own Extension @ MyExtensions.
Mabye you need something like this:
[code]
public static string ToHexString(this IEnumerable<int> numbers)
{
return numbers.Select(x=>string.Format("0x{0:X2}", x)).Aggregate((c,y)=>c + " " + y);
}
[/code]