C# 判讀取得字符編碼格式


            FileStream fs1 = new FileStream(folder + strPath, FileMode.Open);

            byte[] bytes = new byte[fs1.Length];
            fs1.Read(bytes, 0, bytes.Length);
            // 設置當前流的位置為流的開始   
            fs1.Seek(0, SeekOrigin.Begin);

            UniversalDetector Det = new UniversalDetector(null);
            Det.HandleData(bytes, 0, bytes.Length);
            Det.DataEnd();
            //得到文檔字符編碼類型
            string cLX = Det.GetDetectedCharset();
       string Text = Encoding.GetEncoding(cLX).GetString(bytes);
       //第2種方式 StreamReader sr = new StreamReader(fs1, Encoding.GetEncoding(cLX)); string str1 = sr.ReadToEnd();

 UniversalDetector這個是個第三方的字符編碼識別,准確率還是可以的

http://pan.baidu.com/s/1pJ5C6TX   

UniversalDetector 源碼鏈接如上


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM