I am trying to copy a byte stream from a database, encode it and finally display it on a web page. However, I notice different behaviors that encode content in different ways (note: I use the “Western European” encoding with the Latin character set and do not support Chinese characters):
var encoding = Encoding.GetEncoding(1252 ); using (var fileStream = new StreamReader(new MemoryStream(content), encoding)) { var str = fileStream.ReadToEnd(); }
Vs.
var encoding = Encoding.GetEncoding(1252 ); var str = new string(encoding.GetChars(content));
If the content contains Chinese characters than the first block of code, you will get a string like "D $ 教学 而 设计 的", which is wrong, since the encoding should not support these characters, and the second block will create "D $ æ • ™ å | è € Œè®¾è®¡çš "", which is correct because they are all in the Western European character set.
What is the explanation for this difference in behavior?
Sidawy
source share