见题:
有一中文字符串“随便一做”,我想编码 再反编码 ,请教能否给点建议,以及对编码的指导?
String temp = "随便一做";
String tempUTF = null;
tempUTF = new String(temp.getBytes("utf-8"),"gbk");
System.out.println("gbk to utf8 : "+tempUTF);
System.out.println("utf8 to gbk : "+new String(tempUTF.getBytes("gbk"),"utf-8"));BASE64Encoder encoder = new BASE64Encoder(); BASE64Decoder decoder = new BASE64Decoder(); String t1 = encoder.encode(tempUTF.getBytes()); String t2 = new String(decoder.decodeBuffer(t1)).toString(); String t3 = new String(t2.getBytes("gbk"),"UTF-8"); System.out.println("utf8 to base64 : "+t1); System.out.println("base64 to utf8 : "+t2); System.out.println("utf8 to gbk : "+t3); String t4 = encoder.encode(temp.getBytes()); String t5 = new String(decoder.decodeBuffer(t4)).toString(); System.out.println("gbk to base64 : "+t4); System.out.println("base64 to gbk : "+t5);
得出来的结果是:
1)没有得到预期效果:
gbk to utf8 : 闅忎究涓?鍋?
utf8 to gbk : 随便????
2)同上:
utf8 to base64 : 6ZqP5L6/5Lg/5YE/
base64 to utf8 : 闅忎究涓?鍋?
utf8 to gbk : 随便????
3)理想效果:
gbk to base64 : y+ax49K71/Y=
base64 to gbk : 随便一做
问题补充:
对于上面的编码问题,我是想解决从base64——》utf8的效果。其实例有:
有没有人能帮我把以下字符串变成中文:
56CB5bCa5ouN77ya6ZqP5L6/5LiA5YGa
(以上字符串经过base64编码的)
56CB5bCa5ouN77ya6ZqP5L6/5LiA5YGa
(以上字符串经过base64编码的)
其解决方法:
BASE64Decoder decoder = new BASE64Decoder();
String str;
try {
str = new String(decoder.decodeBuffer(s),"utf-8");
System.out.println(str);
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
其结果为:“码尚拍:随便一做 ”
而我原来的方法是:
BASE64Decoder decoder = new BASE64Decoder();
String str = new String(decoder.decodeBuffer(baseStr)).toString();
System.out.println(str);
str = new String(str.getBytes("gbk"),"utf-8");
System.out.println(str);
上面解码的跟下面这个有什么不一样吗?