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