日期:2014-05-20  浏览次数:20800 次

在JAVA中如何将一个字符串转换为国际编码(尤其是中文)
我有个字符串如“test测试”,现在我要如何才能将其转换为国际编码,注意是要在JAVA里的代码实现,不是在外界转换(这个我知道),请高手指点!谢谢

问题补充:可能是我描述的还不够清楚,我说的是国际编码不是字符编码,如“星期一”的国际编码是“\u661f\u671f\u4e00”,我该如何通过JAVA代码转换,谢谢!

------解决方案--------------------
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();
}
}
------解决方案--------------------
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();
}
}