ITeye博客

判断字节数组是否为utf-8编码

floydd · 2008-01-24 16:44 · Markdown

privatestaticvoidisValidUTF8(byte[]bytes)throwsIllegalStateException...{
intcountToValidate=0;
for(inti=0;i<bytes.length&&countToValidate==0;i++)...{
intb=bytes[i];
if(b>0)...{
continue;//asciichar
}
debugInt(b,"cur");
while(((b>>6)&0x3)==0x3)...{//checkthe11pattern
countToValidate++;
b=(b<<1);
debugInt(b,"cur_c");
i++;
if(i>bytes.length-1)break;

intnext=bytes[i]&0xff;
debugInt(next,"next");
if((next>=0x80&&next<0xc0))...{
countToValidate--;
}
}
}
if(countToValidate>0)
thrownewIllegalStateException();
}

参考:
http://blog.csdn.net/sandyen/archive/2006/08/23/1108168.aspx
http://www.eygle.com/digest/2007/01/zhs16gbk_char.html
http://www.linux-cn.com/html/website/html/20070605/52875.html

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。