同志们我这段代码
    public String ifGb2312(String useKey) throws UnsupportedEncodingException {
        if(useKey.equals("")||useKey==null)
        {
            return "";
        }//如果是空则返回空        byte[] getChin = null;
        getChin = useKey.getBytes();
        int iHead = getChin[0] & 0xff;
        int iTail = getChin[1] & 0xff;
        boolean aaa = true;
        aaa = ((iHead >= 0xa1 && iHead <= 0xf7 && iTail >= 0xa1 && iTail <= 0xfe)) ? true : false;
        if (!aaa) {
            useKey = new String(useKey.getBytes("iso-8859-1"), "GB2312");
        }        return useKey;
    }
这段代码虽然可以判断出是不是GB2312,如果不是就从ISO-8859-1转过来,如果是就直接返回。但是别的汉字都能够识别唯独“澳”,“法”,“分”等词不行,我真没有办法了各位看看吧!谢谢啦

解决方案 »

  1.   

    编码不太懂,但是觉得lz这句
    if(useKey.equals("") ¦ ¦useKey==null)
    写成这样比较好点
    if(useKey==null ||useKey.equals(""))
      

  2.   

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

  3.   

    楼上的同志做法我很同意但是现在很着急的不是这个问题主要还是那个转码判断地方的问题为什么说我打“澳大利亚”如果传递参数是“ISO-8859-1”编码返回的仍然是乱码但是我打“大利亚”的ISO-8859-1则返回的是“大利亚”啊?
      

  4.   

    if (ints[0] >= 0x81 && ints[0] <= 0xFE && ints[1] >= 0x40 && ints[1] <= 0xFE) {修改你的代码看看aaa   =   ((iHead   > =   0x81   &&   iHead   <=   0xFE   &&   iTail   > =   0x40   &&   iTail   <=   0xFE))   ?   true   :   false; 
      

  5.   

    等等好像不对了帮我在看看
    import java.io.UnsupportedEncodingException;/**
     * <p>Title: </p>
     *
     * <p>Description: </p>
     *
     * <p>Copyright: Copyright (c) 2007</p>
     *
     * <p>Company: </p>
     *
     * @author not attributable
     * @version 1.0
     */
    public class TestUser {
        public TestUser() {
        }    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;
        }
        public static void main(String[] args) throws UnsupportedEncodingException {
            TestUser tu = new TestUser();
             String sGB = "澳大利亚";
             String gb=sGB;
             gb=new String(gb.getBytes(),"ISO-8859-1");         boolean aa = tu.isGB2312(gb);
             // boolean aa=ig.isGBCode(sGBK);
             //ig.ifISO("");
             System.out.println(aa);    }
    }
      

  6.   

    老紫竹在帮我看看这个程序吧什么打“澳大利亚”转码后就返回true,打“大利亚”转码后就是false,正常的应该是无论什么都返回false啊?
      

  7.   

    我刚明白你要干什么!!提示:
    误解:“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));  }