Java8 base64

2018-11-28
java

Base64

java8 支援 Base64.Decoder, Base64.Encoder, API docs

final Base64.Decoder decoder = Base64.getDecoder();
final Base64.Encoder encoder = Base64.getEncoder();
final String text = "字串文字";
final byte[] textByte = text.getBytes("UTF-8");

//編碼
final String encodedText = encoder.encodeToString(textByte);

//解碼
System.out.println(new String(decoder.decode(encodedText), "UTF-8"));

基本原理

text -> ascii -> 取 6bits -> mapping 64 code -> 空白捕 ‘=’
[https://zh.wikipedia.org/wiki/Base64](base64 wiki : https://zh.wikipedia.org/wiki/Base64)


Comments: