如题:
我在将某个字符串转成 utf-8 后,希望再将其转换为 Unicode 可是得不到希望的结果!public class Test { public static void main(String[] args) throws Exception {
String temp1 = "汉字";
System.out.println("temp1>>"+temp1);
String temp2 = new String(temp1.getBytes("utf-8"));//转utf-8
System.out.println("temp2>>"+temp2);
String temp3 = new String(temp2.getBytes());//想转 Unicode
System.out.println("temp3>>"+temp3); //不能输出 "汉字" 这两个字
}}
我在将某个字符串转成 utf-8 后,希望再将其转换为 Unicode 可是得不到希望的结果!public class Test { public static void main(String[] args) throws Exception {
String temp1 = "汉字";
System.out.println("temp1>>"+temp1);
String temp2 = new String(temp1.getBytes("utf-8"));//转utf-8
System.out.println("temp2>>"+temp2);
String temp3 = new String(temp2.getBytes());//想转 Unicode
System.out.println("temp3>>"+temp3); //不能输出 "汉字" 这两个字
}}
试试,再用这种方面转回Unicode
temp1 是unicode
unicode 要转成utf8 需要先转成iso-8859-1 再转成utf8
String temp2=newString(temp1.getBytes("utf-8"));
是用utf-8解析,用默认gbk构造。所以,temp2是乱码。
你应该这样String temp2=newString(temp1.getBytes("utf-8"),"utf-8");或String temp2=newString(temp1.getBytes("gbk"));
temp2 = new String(temp2.getBytes("unicode"),"unicode");
package enCoding;
public class Test { public static void main(String[] args) throws Exception {
String temp1 = "汉字";
System.out.println("temp1>>"+temp1);
String temp2 = new String(temp1.getBytes("utf-8"),"utf-8");//转utf-8
System.out.println("temp2>>"+temp2);
String temp3 = new String(temp2.getBytes("utf-8"),"utf-8");//想转 Unicode
System.out.println("temp3>>"+temp3); //不能输出 "汉字" 这两个字
}}请注意 new String()的参数,如果没有指定decode 方式,那么采用系统(或编译器)默认的处理,一旦不配套,肯定是显示乱码