So this is a section of code for my tcp client. This part is to convert the bytes recieved into characters. However, i would like to put some logic to it and to do that i need to set this output to a string. As it keeps printing out every character individually how would i do this? If you require anymore information feel free to ask.
byte[] bb = new byte[100];
int k = stm.Read(bb, 0, 100);
for (int i = 0; i < k; i++)
Console.Write(Convert.ToChar(bb[i]));
Thanks in advace.
As per comments, if you want to decode a byte array in a particular encoding, just use Encoding.GetString
. For example:
string text = Encoding.ASCII.GetString(bb, 0, k);
(Note that ASCII is rarely a good choice if the text is meant to be arbitrary human text. UTF-8 is usually a better option at that point, but then you need to bear in mind the possibility that a single character may be split across multiple bytes - and therefore multiple calls to Stream.Read
.)
See more on this question at Stackoverflow