Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
332 views
in Technique[技术] by (71.8m points)

java - Base64 encoder and decoder

Is there a base-64 decoder and encoder for a String in Android?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is an example of how to use the Base64 class to encode and decode a simple String value.

// String to be encoded with Base64
String text = "Test";
// Sending side
byte[] data = null;
try {
    data = text.getBytes("UTF-8");
} catch (UnsupportedEncodingException e1) {
    e1.printStackTrace();
}
String base64 = Base64.encodeToString(data, Base64.DEFAULT);

// Receiving side
byte[] data1 = Base64.decode(base64, Base64.DEFAULT);
String text1 = null;
try {
    text1 = new String(data1, "UTF-8");
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
}

This excerpt can be included in an Android activity.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...