How to read a Unicode data saved as ASCII in notepad file as txt ?

L

Learning.Net

How to read a Unicode data saved as ASCII in notepad file as txt ?
I tried using streamReader but it is not showing Unicode data.
eg.
using (StreamReader sr = new StreamReader(test.txt)
{
string line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
Console.Read();
}
 
J

Jon Skeet [C# MVP]

How to read a Unicode data saved as ASCII in notepad file as txt ?

Do you mean ANSI rather than ASCII? If so, pass Encoding.Default as a
parameter for your StreamReader constructor.

If it's truly saved as ASCII, you won't have any accented characters
etc, and it should read fine as UTF-8.

Jon
 
M

Maate

Hi,

I'm not sure I understand the phrase 'unicode data saved as ASCII' - I
guess either the text is ASCII-encoded, or else it is Unicode-encoded?

No matter what, you need to specify an encoding in your code, e.g.:

System.IO.StreamReader sr = new System.IO.StreamReader("test.txt",
System.Text.Encoding.ASCII)

or

System.IO.StreamReader sr = new System.IO.StreamReader("test.txt",
System.Text.Encoding.UTF8)

You can also try out different encodings from the intellisense if you
don't know how your file is encoded. If your text still looks funny,
try posting a sample. Hope this is any help.

Morten:)
 
J

Jon Skeet [C# MVP]

I'm not sure I understand the phrase 'unicode data saved as ASCII' - I
guess either the text is ASCII-encoded, or else it is Unicode-encoded?

No matter what, you need to specify an encoding in your code, e.g.:

System.IO.StreamReader sr = new System.IO.StreamReader("test.txt",
System.Text.Encoding.ASCII)

or

System.IO.StreamReader sr = new System.IO.StreamReader("test.txt",
System.Text.Encoding.UTF8)

Well, if it *is* in UTF-8 then you don't need to specify the encoding
- UTF-8 is picked by default by pretty much all the .NET classes (in
this case the docs aren't terribly clear, unfortunately).
You can also try out different encodings from the intellisense if you
don't know how your file is encoded. If your text still looks funny,
try posting a sample. Hope this is any help.

Note that if we do end up seeing a sample, it's probably best to post
the hex version of it - just posting the text will involve other
encoding conversions, which kinda defeats the point...

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top