=====[ Code Start / C# ]===== |
Stream reader = File.Open(filename翻譯社 FileMode.Open, FileAccess.Read); // UniCode File int source = reader.Read(buffer, 0翻譯社 1024); string sSource = string.Empty; reader.Read(header, 0, 4); sSource += encoder.GetString(buffer, 0翻譯社 source); encoder = Encoding.Default; source = reader.Read(buffer, 0, 1024); 本Blog裡所有的程式碼,都是經由此程式轉換的,有愛好的伴侶,可以參閱:http://coolfire.fetag.org/?p=219 reader.Position = 3; // .......... 接下來的程式 |
=====[ Code End / C# ]===== |
以下是程式範例:
} else {byte[] header = new byte[4];
if (header[0] == 0xFF && header[1] == 0xFE) {
reader.Close();
// Default Encoding File
}
encoder = Encoding.UTF8;
開辟過程中,碰到一個檔案編碼的問題,File.Open()回傳一個Stream,必需轉成byte[]型態,再用Encoding轉成字串,但要若何知道開啟的文字檔的檔案編碼勒!?據萬國翻譯公司所知,好像沒有class可以做到...
不外我們可以直接從檔案裡去查抄,搜檢前四碼就能夠知道該Stream的編碼類型(Encoding)了...
固然不是只有這三種,這也是為什麼抓四碼的原因,詳細說明請參閱MSDN:
http://msdn2.microsoft.com/en-us/library/system.text.encoding.unicode.aspx
encoder = Encoding.Unicode;
比來在寫個CodeSyntax的程式,其作用是將程式碼轉換成HTML的表示體例... 比方前三碼假如是EF BB BE,那就是UTF-8 File;假如前兩碼是FF FE,則就是UniCode File;假設都不是,則為預設編碼型態(Default)
}
reader.Position = 2;
Encoding encoder = null;
// UTF-8 File
byte[] buffer = new byte[1024];
} else if (header[0] == 0xEF && header[1] == 0xBB && header[2] == 0xBF) {
以下文章來自: http://blog.xuite.net/jaofeng.chen/DesignShow/4702957-%E7%94%A8%E7%A8%8B%E5%BC%8F%E5%88%A4%E6%96%B7%有關各國語文翻譯公證的問題歡迎諮詢萬國翻譯公司02-23690931