程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> utf8編碼檢測辦法分享

utf8編碼檢測辦法分享

編輯:C#入門知識

utf8編碼檢測辦法分享。本站提示廣大學習愛好者:(utf8編碼檢測辦法分享)文章只能為提供參考,不一定能成為您想要的結果。以下是utf8編碼檢測辦法分享正文



public bool isUtf8(byte[] rawText)
        {
            bool result = true;

            if (rawText == null)
            {
                return !result;
            }

            int pos = 0;
            while (pos < rawText.Length && result)
            {
                if ((rawText[pos] & 0x7F) == rawText[pos])
                {
                    pos++;
                }
                else
                {
                    int bitLen = 7;

                    while (((rawText[pos] >> bitLen) & 0x01) == 1 && bitLen > 0)
                    {
                        bitLen--;
                    }

                    int byteCount = 7 - bitLen;

                    if (byteCount > 1 && byteCount < 7)
                    {
                        for (int i = 1; i < byteCount; ++i)
                        {
                            if (pos + i >= rawText.Length || (rawText[pos + i] & 0xBF) != rawText[pos + i])
                            {
                                result = false;
                                break;
                            }
                        }

                        pos += byteCount;
                    }
                    else
                    {
                        result = false;
                    }
                }
            }

            return result;
        }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved