1

I am now having an issue where Celsius symbol gets read as C instead of °C.

Looks like the encoding the culprit. I tried to do this:

            using (StreamReader sr = new StreamReader(this._inFilePath,System.Text.Encoding.Unicode ,true))

instead of

            using (StreamReader sr = new StreamReader(this._inFilePath))

but I am now getting garbage....does the original file encoding have to match the StreamReader encoding? I am using compact framework 2.0.

I have found this online, but if use this I have read it all into a byte array, detect the end of each line,convert it to Unicode, and then proceed with a program logic. Anyone used this class?

5
  • What was the original file encoding? Commented Mar 12, 2009 at 20:05
  • utf-8. if i pass the utf-8 as a parameter to the streamreader, the data is read, but celsius becomes C Commented Mar 12, 2009 at 20:09
  • 1
    so does the file have to be saved in unicode to be read in Unicode? pardon the stupid question Commented Mar 12, 2009 at 20:09
  • 1
    Of course it does. You can convert it to a different encoding after you read it in, but it has to be parsed in the original encoding first. Commented Mar 12, 2009 at 20:16
  • 1
    "does the original file encoding have to match the StreamReader encoding" - hell yeah. Commented Mar 15, 2009 at 18:58

1 Answer 1

2

Yes, you need to specify the correct encoding when you construct your StreamReader. .NET might be able to detect the encoding for you. There are overloads for the StreamReader constructor which take a boolean parameter you can use to request this behavior.

public StreamReader( string path, bool detectEncodingFromByteOrderMarks)

Sign up to request clarification or add additional context in comments.

2 Comments

So basically, if I want the file to be read in Unicode I have to save it in Unicode first, correct?
You need to use an encoding that supports unicode. Normally this is UTF-8 or UTF-16 (which is called "Unicode" in .NET). Be sure not to use ASCII or ANSI (Encoding.Default). You could post another question about saving files, and describe your situation more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.