讀big5編碼的file轉成utf8

摘要:讀big5編碼的file轉成utf8


byte[] big5Bytes = null;

string FilePath = Server.MapPath("~/txt/test.txt");

using (System.IO.FileStream fs = new System.IO.FileStream(FilePath, System.IO.FileMode.Open))
 {
    //讀big5編碼bytes
    big5Bytes = new byte[fs.Length];
    fs.Read(big5Bytes, 0, (int)fs.Length);
 }

//將big5轉成utf8編碼的bytes
byte[] utf8Bytes = System.Text.Encoding.Convert(System.Text.Encoding.GetEncoding("BIG5"), System.Text.Encoding.UTF8, big5Bytes);

//將utf8 bytes轉成utf8字串
System.Text.UTF8Encoding encUtf8 = new System.Text.UTF8Encoding();

string utf8Str = encUtf8.GetString(utf8Bytes);

Response.Write(utf8Str);