我想把GBK表示的"中"转换为UTF-8表示的"中",不知道什么办法会很快捷。现在本人已知的方法只有一种,就是利用写入文件时java自动编码转换来做,这样速度太慢了,而且还有写入文件失败的危险。PS:注意,不是乱码处理,别拿什么String   newstring=new   String(oldstring.getBytes("gbk"),"utf-8");这种答案来敷衍!!!

解决方案 »

  1.   

    首先提示,java在内部都是unicode编码,不存在什么GBK/UTF-8
    只有在输入输出的时候,才会发生与外部的编码转化问题。
    比如传来的是GBK,就得按照GBK编码转化为unicode才行。输出也一样。我不知道你提到的转换用在哪个地方?我能想到的,就是文件转换。以前保存时用GBK编码的,现在想全部转换为UTF-8编码。
    这个用文本工具吧,java也行,用GBk读取文件,用UTF-8输出就行了!不用做任何其他事情
      

  2.   

    我是想用print打出汉字在各种编码的对应码值
      

  3.   

    比方说

    gbk:d5e2
    utf-8:8fd9
      

  4.   

    我给个例子,剩下的自己弄去    String hz = "这";
        byte[] bs = hz.getBytes("GBK");
        for(byte b : bs){
          System.out.print(b);
        }
        bs = hz.getBytes("UTF-8");
        for(byte b : bs){
          System.out.print(b);
        }
    怎么把字节变成字符串?你自己弄吧!
      

  5.   

      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));
        }
      }  public static String byteToString(byte b) {
        byte high, low;
        byte maskHigh = (byte) 0xf0;
        byte maskLow = 0x0f;    high = (byte) ((b & maskHigh) >> 4);
        low = (byte) (b & maskLow);    StringBuffer buf = new StringBuffer();
        buf.append(findHex(high));
        buf.append(findHex(low));    return buf.toString();
      }  private static char findHex(byte b) {
        int t = new Byte(b).intValue();
        t = t < 0 ? t + 16 : t;    if ((0 <= t) && (t <= 9)) {
          return (char) (t + '0');
        }    return (char) (t - 10 + 'A');
      }输出
    D5E2
    E8BF99
      

  6.   

    可是,老紫竹,不知道你有没有看过我的问题,你去查一下,utf-8的内码应该是8fd9,绝不是你的什么e8bf99,只要问题看清楚,你就不会犯你这种错误…………