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.