Let's say we have a byte array called buff
. The first three way works just fine but the 4th way throws and exception. The only difference from the third way is that u give it the offset and length. I am not sure why the fourth way doesn't work.
// First way - works
string converted = Encoding.UTF8.GetString(buff, 0, buff.Length);
Console.WriteLine("converted = " + converted);
// Second way - works
var str = System.Text.Encoding.Default.GetString(buff);
Console.WriteLine("str = " + str);
// Third way - works
string ASCII = Encoding.ASCII.GetString(buff, 0, buff.Length);
Console.WriteLine("ASCII = " + ASCII);
// Fourth way - doesn't work
ASCIIEncoding asciiEncoding = new ASCIIEncoding();
string byteCountString = asciiEncoding.GetString(buff);