Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Encoding Accented Characters

0.00/5 (No votes)
22 May 2007 1  
There is a problem exporting accented characters in plain text files. You need to encode, but which one?

Introduction

There is a problem exporting accented characters in text files. Some programs cannot import or correctly display accented characters. Therefore you need to use encoding to correctly export a plain text file. However, there are a LOT of encodings, so which one should you use?

Here's How

The answer is: iso-8859-8.

That is the Hebrew (ISO-Visual) encoding. The encoding is natively supported in .NET. It intelligently converts to a visual format for you. The other standard encoders do not do this as you will see below.

Example

Converting the following: Frédéric François.

Encoding Description Output
ASCII Fr?d?ric Fran?ois
Default Frédéric François
UTF7 Unicode (UTF-7) Fr+AOk-d+AOk-ric Fran+AOc-ois
UTF8 Unicode (UTF-8) Frédéric François
iso-8859-1 Western European (ISO) Frédéric François
iso-8859-8 Hebrew (ISO-Visual) Frederic Francois
us-ascii US-ASCII Fr?d?ric Fran?ois
Windows-1252 Western European (Windows) Frédéric François

Example of Code Using Encoding

StreamWriter sw = new StreamWriter
    ("somefile.txt", false, System.Text.Encoding.GetEncoding("iso-8859-8"));

A Full Example for the Beginner

using (StreamWriter sw = new StreamWriter
    ("somefile.txt", false, System.Text.Encoding.GetEncoding("iso-8859-8")))
{
    DataSet1TableAdapters.binsTA ta = new DataSet1TableAdapters.binsTA();
    DataSet1.binsDataTable dt = ta.GetData();
    foreach (DataSet1.binsRow row in dt.Rows)
    {
        sw.Write(row.ID.ToString());
        sw.Write("|");
        sw.WriteLine(row.description);
    }
}

History

  • 22nd May, 2007: Initial post

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here