int IsUTF8(const char* str, int length)
{
bool allAsc = true;//是否为ASC
unsigned char ch;//当前字节
int charByte = 0;//某个字符编码字节数
int i = 0;
while( ch = str[i++])
{
if( ((ch & 0x80) == 0) && charByte == 0)//字节最高位为0则为ASC,且当前字符编码字节数为0
continue;
else if( ((ch & 0x80) == 0) && charByte != 0)//字节最高位为0 切字符编码字节数不为0 则不为UTF8
return 0;
else if(charByte == 0)
{
allAsc = false;
if(ch>=0x80)
{
if( ch >= 0xFC && ch <= 0xFD )
charByte = 6;
else if(ch >= 0xF8)
charByte = 5;
else if(ch >= 0xF0)
charByte = 4;
else if(ch >= 0xE0)
charByte = 3;
else if(ch >= 0xC0)
charByte = 2;
else
return 0;
--charByte;
}
}
else
{
if( (ch&0xC0) != 0x80 )
return false;
charByte--;
}
}
if(allAsc)
return -1;
else
return 1;
}