java使⽤BASE64编解码,处理汉字问题
BASE64编解码与hashcode()码⽐较,BASE64的优点:
1.BASE64的汉字编码,是可以解码可逆的;
2.BASE64的汉字编码,不需要要考虑会不会有不同汉字会有相同的编码问题;
⽤途:
1.⽤Redis内存数据库,存储⼤数据问题时,可以使⽤汉字的BASE64编解码作为key,提⾼查询速度,也会节约存储空间;
2.可⽤于替代Map⽤汉字的key;
使⽤BASE64编解码前,要先导⼊ sun.misc.BASE64Decoder.jar
代码如下:
import Decoder.BASE64Decoder;
import Decoder.BASE64Encoder;
public class BASE64Test {
// 将 s 进⾏ BASE64 编码
public static String getBASE64(String s) throws Exception {
if (s == null)
return null;
return (new BASE64Encoder()).Bytes("UTF-8"));
}
// 将 BASE64 编码的字符串 s 进⾏解码
public static String getFromBASE64(String s) throws Exception {
if (s == null)
return null;
BASE64Decoder decoder = new BASE64Decoder();
try {
byte[] b = decoder.decodeBuffer(s);
return new String(b);
} catch (Exception e) {
return null;
}
}
public static void main(String[] args) throws Exception {
String str = "⾯⼦";
System.out.BASE64(str));
System.out.BASE64(str)));
String str2 = "⾯⼦,新闻,河北,珠⼦";
System.out.BASE64(str2));
decoderSystem.out.BASE64(str2)));
}
}
结果:
6Z2i5a2Q
⾯⼦
6Z2i5a2QLOaWsOmXuyzmsrPljJcs54+g5a2Q ⾯⼦,新闻,河北,珠⼦
如有不恰当之处,请指教

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。