我有个字符串如“test测试”,现在我要如何才能将其转换为国际编码,注意是要在JAVA里的代码实现,不是在外界转换(这个我知道),请高手指点!谢谢问题补充:可能是我描述的还不够清楚,我说的是国际编码不是字符编码,如“星期一”的国际编码是“\u661f\u671f\u4e00”,我该如何通过JAVA代码转换,谢谢!

解决方案 »

  1.   

    package squall.file;import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;public class EncodingPrint { /**
     * @param args
     */
    public static void main(String[] args) throws Exception{
          String tmp = "星期一";
          byte[] src = tmp.getBytes();
          InputStreamReader in = new InputStreamReader(new ByteArrayInputStream(src), "GBK");
          ByteArrayOutputStream buffer = new ByteArrayOutputStream(24);
          OutputStreamWriter out = new OutputStreamWriter(buffer, "UTF-16");
    int i = 0;
    i = in.read();
    while(i != -1)
    {
    out.write(i);
    i = in.read();
    }
    in.close();
    out.flush();
    byte[] result = buffer.toByteArray();
    for(int j = 0 , length = result.length; j < length; ++j)
    System.out.println(Integer.toHexString((int)result[j]));

    out.close();
    }
    }
      

  2.   

    其实是个字符编码的问题,你所说的国际编码是UTF-16,一般我们中文windows的编码是gbk
      

  3.   

    java重的字符都是unicode编码,根本不需要转
      

  4.   

    to Squall1009(钰枫)(找工作ing):单纯的汉字字符串,像你那样做可以,可是如果字符串是字母数字汉字及其它字符的混合体呢??那就乱套了,还有更好的方法吗???,在外部命令中可以用native2ascii.exe来转换,在JAVA程序中就不能直接处理吗???thanks
    hima
      

  5.   

    再重新看一下api,直接package squall.file;
    public class EncodingPrint { /**
     * @param args
     */
    public static void main(String[] args) throws Exception{
          String tmp = "星期一";
          byte[] src = tmp.getBytes("UTF-16");
      for(int i = 0 , length = src.length; i < length; ++i)
    System.out.println(Integer.toHexString((int)src[i]));
    }
    }就可以了
      

  6.   

    如果是b/s的话 全部数据转成unicode字符串即可 不过比较浪费格式1:
    char c='如'
    Integer i=(int)a
    String s="&#"+i.toString()+";"格式2:
    char c='如'
    Integer i=(int)a
    String s="\\u"+i.toHexString()
      

  7.   

    你需要的native2ascii.exe生成的文件格式好象是让java来读的东西,并不是什么UTF-16编码,不过自己写个程序实现也很简单
      

  8.   

    package squall.file;
    public class EncodingPrint { /**
     * @param args
     */
    public static void main(String[] args) throws Exception{
          String tmp = "abcd星期一edf12";
          StringBuffer buffer = new StringBuffer();
          for(int i = 0, length = tmp.length(); i < length; ++i)
          {
           char c = tmp.charAt(i);
           if((c < 127))
           buffer.append(c);
           else
           {
           buffer.append("\\u");
           buffer.append(Integer.toHexString((int)c));
           }
          }
          System.out.println(buffer);
    }
    }
      

  9.   

    class Test 
    {
    public static void main(String[] args) 
    {
    String s="dffj我 ";
    StringBuffer b=new StringBuffer();
    for(char tc:s.toCharArray()){
    String hexString=formatString(tc);
        b.append("\\u"+hexString);
    }
    System.out.println(b);
    }
    public static String formatString(char c){
          String t=Integer.toHexString(c);
      return new StringBuffer("0000").replace(4-t.length(),4,t).toString();
    }
    }