Connecting Tech Pros Worldwide Help | Site Map

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

Learning.Net
Guest
 
Posts: n/a
#1: Aug 8 '07
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();
}

Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#2: Aug 8 '07

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


On Aug 8, 1:06 pm, "Learning.Net" <sanjay.btech2...@gmail.comwrote:
Quote:
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

Maate
Guest
 
Posts: n/a
#3: Aug 8 '07

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



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:)

Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#4: Aug 8 '07

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


On Aug 8, 3:34 pm, Maate <ma...@retkomma.dkwrote:
Quote:
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).
Quote:
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

Closed Thread


Similar C# / C Sharp bytes