Hex, Linq-fu:
string.Concat(ba.Select(b => b.ToString("X2")).ToArray())
UPDATE with the times
As noted by @RubenBartelink, the code that don't have a conversion of IEnumerable
to an array: ba.Select(b => b.ToString("X2"))
does not work prior to 4.0, the same code is now working on 4.0.
This code...
byte[] ba = { 1, 2, 4, 8, 16, 32 };
string s = string.Concat(ba.Select(b => b.ToString("X2")));
string t = string.Concat(ba.Select(b => b.ToString("X2")).ToArray());
Console.WriteLine (s);
Console.WriteLine (t);
...prior to .NET 4.0, the output is:
System.Linq.Enumerable+c__Iterator10`2[System.Byte,System.String]
010204081020
On .NET 4.0 onwards, string.Concat has an overload that accepts IEnumerable. Hence on 4.0, the above code will have same output for both variables s and t
010204081020
010204081020
Prior to 4.0, ba.Select(b => b.ToString("X2"))
goes to overload (object arg0)
, the way for the IEnumerable
to go to a proper overload, i.e. (params string[] values)
, is we need to convert the IEnumerable
to string array. Prior to 4.0, string.Concat has 10 overload functions, on 4.0 it is now 12