Encoding.Default and Encoding.UTF8

H

Hardy Wang

Hi,
I have following code:
Encoding mode; // Encoding.Default or Encoding.UTF8
FileStream sb = new FileStream(fullPathAndFileName, FileMode.Create);
StreamWriter sw = new StreamWriter(sb, mode);
sw.Write(textContent);
sw.Close();

My question is under what situation, the saved files are different by
calling Encoding.Default and Encoding.UTF8.
 
J

Jon Skeet [C# MVP]

Hardy Wang said:
I have following code:
Encoding mode; // Encoding.Default or Encoding.UTF8
FileStream sb = new FileStream(fullPathAndFileName, FileMode.Create);
StreamWriter sw = new StreamWriter(sb, mode);
sw.Write(textContent);
sw.Close();

My question is under what situation, the saved files are different by
calling Encoding.Default and Encoding.UTF8.

They'll almost certainly be different for any string containing non-
ASCII characters. They're likely to be the same for any string
containing solely ASCII characters.

See http://www.pobox.com/~skeet/csharp/unicode.html for more
information.
 
H

Hardy Wang

Thanks,
Anybody knows are there any characters in French and Spanish greater than
128 ASCII code?
 
J

Jon Skeet [C# MVP]

Hardy Wang said:
Anybody knows are there any characters in French and Spanish greater than
128 ASCII code?

There are no such thing as ASCII values above 127. What did you mean,
exactly?
 
H

Hardy Wang

Let me make it clear, I am just wandering if there are any characters in
French and Spanish non ASCII character.

What I would like to know is, if there are such characters, by using
Encoding.Default (US English or Canada English as Win2000's locale) to save
to a text file, will these characters be lost?

Thanks!
 
J

Jon Skeet [C# MVP]

Hardy Wang said:
Let me make it clear, I am just wandering if there are any characters in
French and Spanish non ASCII character.

Certainly - any accented characters, to start with.
What I would like to know is, if there are such characters, by using
Encoding.Default (US English or Canada English as Win2000's locale) to save
to a text file, will these characters be lost?

Possibly. However, there are various characters in Encoding.Default
which *aren't* in ASCII, and which won't be lost.

I'd strongly recommend using UTF-8 if you have the opportunity,
however.
 

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

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top