i am trying to display and update simplified chines(GB2312) charset using base64 , Base64 to GB2312 is working fine but i am not able convert GB2312 to 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);
Aactual result is
GB2312 = 装箱时请拍照
Base64 =6KOF566x5pe26K+35ouN54Wn
Expected result is
GB2312 = 装箱时请拍照
Base64 = 17DP5Mqxx+vFxNXV
You should specify the character set in the call to getBytes()
when you are converting the string to its GB2312 encoding:
String baseString=new String(base64.encode(gb2312.getBytes("GB2312")));
In principle (and to be completely safe) you should do it when you are converting from Base64 (str.getBytes()
), but the default character encoding is probably okay since base-64 encoding uses a subset of US-ASCII. Who knows—you might be running on a platform where the default encoding is EBCDIC.