String str = "中华人民共和国";
String str1 = new String(str.getBytes("utf-8"),"gbk");
System.out.println("str=="+str);
System.out.println("str1=="+str1);
String str2 = new String(str1.getBytes("gbk"),"utf-8");
System.out.println("str2=="+str2);----------------------------------------------
str==中华人民共和国
str1==涓崕浜烘皯鍏卞拰鍥?
str2==中华人民共和??如果str是单数的汉字的话,最后转回来不能转全,如果str是双数的汉字的话就可以转全,有什么解决的办法吗?
String str1 = new String(str.getBytes("utf-8"),"gbk");
System.out.println("str=="+str);
System.out.println("str1=="+str1);
String str2 = new String(str1.getBytes("gbk"),"utf-8");
System.out.println("str2=="+str2);----------------------------------------------
str==中华人民共和国
str1==涓崕浜烘皯鍏卞拰鍥?
str2==中华人民共和??如果str是单数的汉字的话,最后转回来不能转全,如果str是双数的汉字的话就可以转全,有什么解决的办法吗?
简单就是美。String str = "中华人民共和国";
这是双字节unicode编码。
str.getBytes("utf-8")
unicode转为utf-8
new String(str.getBytes("utf-8"),"gbk");
utf-8转为gbk
好像没什么意义。
转码因为编码方式的不同,并不是说像LZ这样转过去然后在转回来就能得到正确的结果的.
由于编码方式的不同,在转码过程中可能有一些信息已经丢失.
String str1 = new String((str+"@").getBytes("utf-8"),"gbk");
System.out.println("str=="+str);
System.out.println("str1=="+str1);
String str2 = new String((str1).getBytes("gbk"),"utf-8");
System.out.println("str2=="+str2.substring(0,str2.length()-1));
String都可以用UTF-8变成byte
byte都可以用ISO-8859-1变成String
new String("STR".getBytes("UTF-8"),"UTF-8")
(new String(byte[],"ISO-8859-1")).getBytes("ISO-8859-1")
是没问题的
不一定的
byte[] 到 string 用GBK的转换方式是
0-128 的byte 转换成一个unicode字符
128-256的byte加上后面的一个字节 转换成一个unicode字符
byte[] a = str.getBytes("gbk");
然后我把a进行传递,传到后,
用String strnew= new String(str,"gbk");
现在就没乱码了。
我的问题是传递一个String的参数,接收后变成乱码了,乱码和这个转换一样new String(str.getBytes("utf-8"),"gbk");
所以前面我才想到再给转回来,不想转回来有问题,不知道大家看懂没,
照你这么说new String("STR".getBytes("UTF-8"),"UTF-8")
(new String(byte[],"ISO-8859-1")).getBytes("ISO-8859-1")
没问题,
new String("STR".getBytes("gbk"),"gbk") 有问题了?
如果str是单数的汉字的话,最后转回来不能转全,如果str是双数的汉字的话就可以转全,有什么解决的办法吗?
不一定的
byte[] 到 string 用GBK的转换方式是
0-128 的byte 转换成一个unicode字符
128-256的byte加上后面的一个字节 转换成一个unicode字符
照你这么说new String("STR".getBytes("UTF-8"),"UTF-8")
(new String(byte[],"ISO-8859-1")).getBytes("ISO-8859-1")
没问题,
new String("STR".getBytes("gbk"),"gbk") 有问题了?
如果 "STR"不是GBK编码,是其它的字符集,就可能有问题,因为GBK并没有覆盖整个UNICODE 16字符集
这时原序列已经被损坏,是没有办法再还原的。