日期:2014-05-18 浏览次数:21062 次
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;
}
------解决方案--------------------
if (ints[0] >= 0x81 && ints[0] <= 0xFE && ints[1] >= 0x40 && ints[1] <= 0xFE) {
修改你的代码看看
aaa = ((iHead > = 0x81 && iHead <= 0xFE && iTail > = 0x40 && iTail <= 0xFE)) ? true : false;
------解决方案--------------------
我刚明白你要干什么!!
提示:
误解:“Java 中,怎样知道某个字符串的内码?”
Java 中,字符串类 java.lang.String 处理的是 UNICODE 字符串,不是 ANSI 字符串。我们只需要把字符串作为“抽象的符号的串”来看待。因此不存在字符串的内码的问题。
也就是说,一旦你生成了java String 对象,就无法知道原始编码是啥了!
同样的2个字节,在不同的编码方式下,可能都能使用。
比如汉字 "这"
其GKB编码为D5E2
其UTF-8编码为 E8 BF 99
你知道 "杩"的GK编码是啥嘛? 是 E8 BF, 也就是UTF-8编码的前2位, 所以从字节上根本不能判断是什么编码的!
给你一个测试编码的程序,你看一下就知道了!
public static void main(String[] agrs) throws Exception {
String hz = "这";
byte[] bs = hz.getBytes("GBK");
for (byte b : bs) {
System.out.print(byteToString(b));
}
System.out.println();
bs = hz.getBytes("UTF-8");
for (byte b : bs) {
System.out.print(byteToString(b));
}
bs = new byte[]{(byte)0xE8,(byte)0xBF};
System.out.println(new String(bs));
}