Byte[] to ASCII
Solution 1:
Use:
System.Text.Encoding.ASCII.GetString(buf);
Solution 2:
You can use:
System.Text.Encoding.ASCII.GetString(buf);
But sometimes you will get a weird number instead of the string you want. In that case, your original string may have some hexadecimal character when you see it. If it's the case, you may want to try this:
System.Text.Encoding.UTF8.GetString(buf);
Or as a last resort:
System.Text.Encoding.Default.GetString(bytearray);
Solution 3:
Encoding.ASCII.GetString(buf);
Solution 4:
As an alternative to reading a data from a stream to a byte array, you could let the framework handle everything and just use a StreamReader
set up with an ASCII encoding to read in the string. That way you don't need to worry about getting the appropriate buffer size or larger data sizes.
using (var reader = new StreamReader(stream, Encoding.ASCII))
{
string theString = reader.ReadToEnd();
// do something with theString
}