This part of
VR Karthikeyan's code:
string str = BitConverter.ToString(e.Data);
txtData.AppendText(str);
To get the desirable output
00 00 e2 00 ......, change the above block to use string formatter "X" like this:
foreach (byte i in e.Data)
{
string str2 = i.ToString("X2").Append(" ");
txtData.AppendText(str2);
}
Yet this executes slowly due to appending text to
txtData which is a textBox. When byte[] is large the speed difference shows. In that case use this technique to improve perceived performance, mainly by moving the
.AppendText() operation outside of the loop:
using System.Text;
StringBuilder s = new StringBuilder();
foreach (byte i in e.Data)
{
s.Append(i.ToString("X2")).Append(" ");
}
txtData.AppendText(s.ToString());
The technique employed
StringBuilder to collect conversion results of all the bytes in the array
e.Data. When the foreach loop finishes,
txtData is appended in one go. To wrap this up,
VR Karthikeyan's code can be written like this:
using System.Text;
txtData.AppendText(OurOwnConverter(e.Data));
string OurOwnConverter(byte[] byteArray)
{
StringBuilder s = new StringBuilder();
foreach (byte i in byteArray)
{
s.Append(i.ToString("X2")).Append(" ");
}
return s.ToString();
}