Wednesday, January 27, 2010

C# - example of UTF8 encoding

1) Command to read file which is encoded in UTF8

StreamReader sread = new StreamReader(resStream, Encoding.UTF8);

2) Command to read file whichis encoded in GB2312

StreamReader sread = new StreamReader(resStream, Encoding.GetEncoding("GB2312"));

3) Code below to create a file in UTF8

FileStream hStream = null;
FileInfo hFile = new FileInfo(strPath);

iResult = 0;
try
{
hStream = hFile.Create();

/*
szHeader = new byte[10];
szHeader[0] = 0xef;
szHeader[1] = 0xbb;
szHeader[2] = 0xbf; // or 0xef, 0xbc, 0xbb
hStream.Write(szHeader, 0, 3);
*/
//szMess = new System.Text.UTF8Encoding().GetBytes(strContent);

//false: without UTF-8 BOM header, true:with BOM header
szMess = new System.Text.UTF8Encoding(false).GetBytes(strContent);

hStream.Write(szMess, 0, szMess.Length);
}
catch (Exception ex)
{
....
}

if (hStream != null)
{
hStream.Flush();
hStream.Close();
}

No comments: