Search code examples
c#.netunicode-string

.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


Solution

  • The parameter detectEncodingFromByteOrderMarks should be set to false when creating StreamReader object.