Encoding and Decoding in C# with .NET Framework
Content-Type: text/plain; charset=ISO-8859-1
Which is simply "ISO-8859-1" corresponds to code page 28591, "Western European (ISO)". With globalization it is now necessary to accommodate multiple languages and symbols as a result we have Unicode that contains massive number of characters. In order to encode or decode pages we use System.Text namespace. Following conversions are supported by the System.Text: UTF32Encoding for 32-bit integer characters, UnicodeEncoding for 16-bit integer characters, UTF8Encoding for 8-bit integer characters, ASCIIEncoding for ASCII encoding, System.Text.Encoding for ANSI/ISO
Public byte[] ASCIIEncode(string myRegionalData)
{
ASCIIEncoding myEncode = new ASCIIEncoding();
return myEncode.GetBytes(myRegionalData);
}
UTF is Unicode Transformation Format and one of two mapping methods for Unicode.
UTF-7 — is a relatively unpopular 7-bit encoding.
UTF-8 — an 8-bit, variable-width encoding also called max ASCII since it encompasses all ASCI characters.
UTF-EBCDIC — an 8-bit variable-width encoding, which maximizes compatibility with EBCDIC. (not part of The Unicode Standard).
UTF-16 — a 16-bit, variable-width encoding.
UTF-32 — a 32-bit, fixed-width encoding.
While writing to a file we can set up encoding type in C#.
StreamWriter myStreamUTF = new StreamWriter("utfFile.txt", false, Encoding.UTF8);
myStreamUTF.WriteLine("Hello, World!");
myStreamUTF.Close();
By selecting default option for StreamWriter ASP.NET automatically will put in UTF16 which is Unicode. While reading a file you don’t need to specify Unicode with one exception. You need to specify encoding for the type UTF7.
string fileName = "myFile.txt";
StreamWriter myStream = new StreamWriter(fileName, false, Encoding.UTF7);
myStream.WriteLine("Hello, World!");
myStream.Close();
StreamReader myReader = new StreamReader(fileName);
Console.WriteLine(myReader.ReadToEnd());
myReader.Close();