utf8编码检测 utf8编码检测方法分享

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

utf8编码检测 utf8编码检测方法分享

  2021-03-19 我要评论
想了解utf8编码检测方法讲解的相关内容吗,在本文为您仔细讲解utf8编码检测的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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;
        }

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们