七星彩开奖结果对比器:判断字符串的编码类型(GBK / UTF8)

来源:百度文库 编辑:中财网 时间:2024/04/28 14:45:53
GBK:_____________________________________________________________int isgbk(char *s, size_t ns)
{
        if(ns > 2 && (uint8_t)*s >= 0x81 && (uint8_t)*s <= 0xfe
                && (
                        ((uint8_t)*(s+1) >= 0x80 && (uint8_t)*(s+1) <= 0x7e)
                        || ((uint8_t)*(s+1) >= 0xa1 && (uint8_t)*(s+1) <= 0xfe)
                    )
          )
        {
                return 1;
        }
        return 0;
}_______________________________________________________________ bool isGBKCode(const string& strIn)
{
    unsigned char ch1;
    unsigned char ch2;

    if (strIn.size() >= 2)
    {
        ch1 = (unsigned char)strIn.at(0);
        ch2 = (unsigned char)strIn.at(1);
        if (ch1>=129 && ch1<=254 && ch2>=64 && ch2<=254)
            return true;
        else return false;
    }
    else return false;
}
______________________________________________________________  UTF8:  ______________________________________________________________int isutf8(char *s, size_t ns)
{
        uint8_t x = 0, i = 0, j = 0, nbytes = 0, n = 0;

        for(i = 1; i < 7; i++)
        {
                x = (uint8_t)(255 << i);
                if(((uint8_t)*s & x) == x)
                {
                        n = nbytes = (8 - i);
                        for(j = 0; (j < nbytes && j < ns); j++)
                        {
                                if((uint8_t)s[j] <= 0x80 && (uint8_t)s[j] >= 0xc0)break;
                                else n--;
                        }
                        if(n == 0) return nbytes;
                }
        }
        return 0;
}