本帖最后由 燿陚√揚葳 于 2014-7-5 15:57 编辑
当发生解码乱码时,可以先编码,然后用默认的gbk解码
- public class PipedDemo {
- public static void main(String[] args)throws Exception{
- String s="你好";
- byte[] b1=s.getBytes("gbk");
- sop(Arrays.toString(b1));
- String s1=new String(b1,"ISO8859-1");
- sop("s1="+s1);
- //对s1进行iso8859-1编码
- byte[] b2=s1.getBytes("ISO8859-1");
- sop(Arrays.toString(b2));
- //再进行解码
- String s2=new String(b2,"gbk");
- sop("s2="+s2);
- }
- public static void sop(Object obj){
- System.out.println(obj);
- }
- }
复制代码 可是如果两个都是识别中文的编码表,那么用上面的方法结果还是乱码
- public class Demo {
- public static void main(String[] args)throws Exception{
- String s="你好";
- byte[] b1=s.getBytes("gbk");
- sop(Arrays.toString(b1));
- String s1=new String(b1,"UTF-8");
- sop("s1="+s1);
- //对s1进行UTF-8编码
- byte[] b2=s1.getBytes("UTF-8");
- sop(Arrays.toString(b2));
- //再进行解码
- String s2=new String(b2,"gbk");
- sop("s2="+s2);
- }
- public static void sop(Object obj){
- System.out.println(obj);
- }
- }
复制代码 用UTF-8编码后再用gbk解码结果还是乱码,原因是什么呢?有什么解决办法吗?
|
|