位元組流編碼擷取原來這麼複雜

來源:互聯網
上載者:User

一)需求
很多情況下我們需要知道位元組流的編碼,比如
1) 使用編輯器開啟文字檔的時候,編輯器需要識別文字檔的各種編碼
2) 上傳檔案後,分析上傳檔案位元組流需要知道它的編碼

二)探討
不過C#目前還沒有現成的函數能夠擷取,經過和同事的探討,發現UTF8檔案都有一個3位元組的頭,為“EF BB BF”(稱為BOM--Byte Order Mark),判斷這個頭資訊不就可以解決了嗎?代碼如下:

//判斷上傳的檔案的編碼是否是UTF8,buff為上傳檔案的位元組流
    enc     = Encoding.UTF8;
    testencbuff   = enc.GetPreamble();
    if(fileLength>testencbuff.Length && testencbuff[0] == buff[0] && testencbuff[1]==buff[1] && testencbuff[2]==buff[2])
    {
        // 是 UTF8編碼
        string buffString  = enc.GetString(buff);
    }
不過後來發現,不是所有的UTF8編碼的檔案都有BOM資訊,那如何解決呢?

三)最終的方案
沒有BOM資訊只有通過逐個位元組比較的方式才能解決。幸好已經有人解決這個問題了。推薦大家看:
http://dev.csdn.net/Develop/article/10/10961.shtm
http://dev.csdn.net/Develop/article/10/10962.shtm
這裡判斷所有的編碼,基本上都是通過位元組比較的方式。java代碼很容易移植到.NET上,下面是UTF8判斷部分的C#代碼:

  int utf8_probability(byte[] rawtext)
  {
   int score = 0;
   int i, rawtextlen = 0;
   int goodbytes = 0, asciibytes = 0;

   // Maybe also use UTF8 Byte Order Mark:  EF BB BF

   // Check to see if characters fit into acceptable ranges
   rawtextlen = rawtext.Length;
   for (i = 0; i < rawtextlen; i++)
   {
    if ((rawtext[i] & (byte)0x7F) == rawtext[i])
    {  // One byte
     asciibytes++;
     // Ignore ASCII, can throw off count
    }
    else
    {
     int m_rawInt0 = Convert.ToInt16(rawtext[i]);
     int m_rawInt1 = Convert.ToInt16(rawtext[i+1]);
     int m_rawInt2 = Convert.ToInt16(rawtext[i+2]);

     if (256-64 <= m_rawInt0 && m_rawInt0 <= 256-33 && // Two bytes
      i+1 < rawtextlen &&
      256-128 <= m_rawInt1 && m_rawInt1 <= 256-65)
     {
      goodbytes += 2;
      i++;
     }
     else if (256-32 <= m_rawInt0 && m_rawInt0 <= 256-17 && // Three bytes
      i+2 < rawtextlen &&
      256-128 <= m_rawInt1 && m_rawInt1 <= 256-65 &&
      256-128 <= m_rawInt2 && m_rawInt2 <= 256-65)
     {
      goodbytes += 3;
      i+=2;
     }
    }
   }

   if (asciibytes == rawtextlen) { return 0; }

   score = (int)(100 * ((float)goodbytes/(float)(rawtextlen-asciibytes)));

   // If not above 98, reduce to zero to prevent coincidental matches
   // Allows for some (few) bad formed sequences
   if (score > 98)
   {
    return score;
   }
   else if (score > 95 && goodbytes > 30)
   {
    return score;
   }
   else
   {
    return 0;
   }

  }

參考資料:
字元檢測程式(上) 檢測GB2312、BIG5...    
http://dev.csdn.net/Develop/article/10/article/10/10961.shtm
Hello Unicode ——JAVA的中文處理學習筆記
http://www.chedong.com/tech/hello_unicode.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.