Reputation: 909
I want to print an array as hex. In the following code 'buffer' has been filled by 'XBaseRead'. I can print in hex using a foreach loop and the format "{0:X2}" but when I use string.join method with the same format it prints in decimal.
Here is the code:
byte[] buffer = new byte[160];
XBaseFunctions.XBaseRead(df2500VENDR, buffer, 160, false, XBaseFunctions.O_FLAG._O_BINARY);
foreach (byte i in buffer)
{
Console.Write("{0:X2} ", i);
}
Console.WriteLine("\n");
Console.WriteLine("{0:X2}", string.Join(", ", buffer));
Here is the output:
-----------------Dump of raw buffer-----------------
F0 F0 F0 F0 C1 F0 F1 F0 C1 40 C1 D5 E3 C8 D6 D5 E8 40 50 40 E2 D6 D5 E2 40 C9 D5
C3 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 F1 F4 F5 F0 40 E
6 40 F2 F1 E2 E3 40 E2 E3 5C C5 D9 C9 C5 6B 40 D7 C1 40 40 40 F1 F6 F5 F0 F2 40
40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40
40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 00 00 00 00 00 0
0 00 00 00 00 00 00 F9 60 03 A7 7E 3E 7E 3F 00 00 00 00 00 00 00 00 40 40 40 40
240, 240, 240, 240, 193, 240, 241, 240, 193, 64, 193, 213, 227, 200, 214, 213, 2
32, 64, 80, 64, 226, 214, 213, 226, 64, 201, 213, 195, 64, 64, 64, 64, 64, 64, 6
4, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 241, 244, 245, 240, 64, 2
30, 64, 242, 241, 226, 227, 64, 226, 227, 92, 197, 217, 201, 197, 107, 64, 215,
193, 64, 64, 64, 241, 246, 245, 240, 242, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 249, 96, 3, 167, 126, 62, 126, 63, 0, 0, 0, 0,
0, 0, 0, 0, 64, 64, 64, 64
So the question is Why does the {0:X2} work for the foreach loop but no for the string.join method?
Also, if someone could tell me how to limit each line to 16 bytes then I would appreciate that information too.
Upvotes: 0
Views: 5628
Reputation: 11238
You should apply ToString("X2")
on every byte first :
Console.WriteLine(string.Join(", ", buffer.Select(b => b.ToString("X2"));
Upvotes: 6
Reputation: 10162
Because when you're doing a foreach loop, you're passing a byte to the formatted Console.WriteLine(). However, you're passing a string of the entire joined buffer in the other instance, as string.Join(", ", buffer)
returns a string.
foreach (byte i in buffer)
{
Console.Write("{0:X2} ", i); // <- A byte is being passed.
}
Console.WriteLine("{0:X2}", string.Join(", ", buffer)); // <- A string is being passed.
I would do use this:
var str = string.Join(", ", buffer.Select(x => string.Format("{0:X2}", x)));
Console.WriteLine(str);
...or this:
var str = string.Concat(buffer.Select(i => string.Format("{0:X2}, ", i)))
Console.WriteLine(str);
Although the first one doesn't give a trailing comma, so I would use that over the second method. Thought I'd share the different ways of accomplishing it, though.
Hopefully you're familiar with Linq, so I'm not going to go into great detail as far as what's going on, as it's pretty self-explanatory if you understand Linq.
Upvotes: 3