简体   繁体   中英

.NET StreamReader encoding behaviour

I am trying to understand the unicode encoding behaviour and came across the following,

I am writing to a file a string using Encoding.Unicode using

StreamWriter(fileName,false, Encoding.Unicode);

I am reading from the same file but use ASCII intentionally.

 StreamReader(fileName,false, Encoding.ASCII);

When I read the string using ReadLine to my surprise it is giving back the same unicode string.

I expected the string to contain ? or other characters with double the length of the original string.

What is happening here?

Code Snippet

string test= "سشصضطظع";//some random arabic set
StreamWriter s = new StreamWriter(fileName,false, Encoding.UTF8);
s.Write(input);
s.Flush();
s.Close();
StreamReader s = new StreamReader(fileName, encoding);
string ss  = s.ReadLine();
s.Close();
//In string ss I expect to be a ascii with Double the length of test

If I call StreamReader s = new StreamReader(fileName, encoding, false); then it gives the expected result.`

Thanks

创建StreamReader对象时,应将参数detectEncodingFromByteOrderMarks设置为false。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM