由于各处转载已无法找到原创作者,请作者见谅!
对于gb2312来讲,首字节码位从0×81至0×FE,尾字节码位分别是0×40至0×FE,所以 :
/**
* 对于gb2312来讲,首字节码位从0×81至0×FE,尾字节码位分别是0×40至0×FE
*
* @param str
* @return
*/
public static boolean isGB2312(String str) {
char[] chars = str.toCharArray();
boolean isGB2312 = false;
for (int i = 0; i < chars.length; i++) {
byte[] bytes = ("" + chars[i]).getBytes();
if (bytes.length == 2) {
int[] ints = new int[2];
ints[0] = bytes[0] & 0xff;
ints[1] = bytes[1] & 0xff;
if (ints[0] >= 0x81 && ints[0] <= 0xFE && ints[1] >= 0x40
&& ints[1] <= 0xFE) {
isGB2312 = true;
break;
}
}
}
return isGB2312;
}
本文介绍了一种用于检测字符串是否为GB2312编码的方法。通过遍历字符串中的每个字符并检查其字节范围来判断是否符合GB2312的编码规则。
9957

被折叠的 条评论
为什么被折叠?



