Base64 encoder and decoder

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) {
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) {

This excerpt can be included in an Android activity.

Leave a Comment