欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

utf8编码检测方法分享

程序员文章站 2023-12-20 15:58:28
复制代码 代码如下:public bool isutf8(byte[] rawtext)       ...

复制代码 代码如下:

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;
        }

上一篇:

下一篇: