I'm reading a stream and am wondering why the UTF-8 encoded string is shorter than the ASCII one.
ASCIIEncoding encoder = new ASCIIEncoding();
UTF8Encoding enc = new UTF8Encoding();
string response = encoder.GetString(message, 0, bytesRead); //4096
string responseUtf8 = enc.GetString(message, 0, bytesRead); //3955
That's because the stream is actually UTF-8 encoded. If it was ASCII encoded, the strings would be identical.
When read as ASCII, the byte combinations that represent characters outside the 0-127 code set will be read as separate characters, and they will look like garbage.
When read as UTF-8, the byte combinations will be decoded into the correct characters, each multi-byte combination ending up as a single character.
(Note: Strings are not encoded, it's the stream that is encoded. You decode the stream from ASCII or UTF-8 into a Unicode character string.)