我试图使用GB2312显示和更新简化汉字( base64 )字符集,Base64到GB2312运行良好,但无法将GB2312转换为base64。
String str="17DP5Mqxx+vFxNXV";
Base64 base64=new Base64();
String gb2312=new String(base64.decode(str.getBytes()),"GB2312");
System.out.println("GB2312 = "+gb2312);
String baseString=new String(base64.encode(gb2312.getBytes()));
System.out.println("Base64 = "+baseString);的实际结果是
GB2312 =装箱时请拍照
Base64 =6KOF566x5pe26K+35ouN54Wn
预期结果为
GB2312 =装箱时请拍照
Base64 = 17DP5Mqxx+vFxNXV
发布于 2012-12-28 21:03:53
在将字符串转换为getBytes()编码时,应该在调用GB2312时指定字符集:
String baseString=new String(base64.encode(gb2312.getBytes("GB2312")));原则上(为了完全安全起见),您在从Base64 (str.getBytes())转换时应该这样做,但是默认的字符编码可能是可以的,因为base-64编码使用了uses的一个子集。谁知道呢--您可能运行在一个默认编码为EBCDIC的平台上。
https://stackoverflow.com/questions/14075711
复制相似问题