Base64 encoder and decoder

JavaAndroidBase64EncoderDecoder

Java Problem Overview


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

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

See android.util.Base64

It seems that this was added in API version 8 or android 2.2 so it will not be available on the older platforms.

But the source of it is at android/util/Base64.java so if needed one could just copy it unchanged for older versions.

Solution 3 - Java

Here is a simple method I was going to use until I realized that this is only supported in Android API 8+:

// Has line break
public String getBase64(String input) {
    return Base64.encodeToString(input.getBytes(), Base64.DEFAULT);
}

// No line break
public String getBase64(String input) {
    return Base64.encodeToString(input.getBytes(), Base64.NO_WRAP);
}

Solution 4 - Java

To encode:
private String encodeString(String s) {
    byte[] data = new byte[0];

    try {
        data = s.getBytes("UTF-8");

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } finally {
        String base64Encoded = Base64.encodeToString(data, Base64.DEFAULT);

        return base64Encoded;

    }
}
To decode:
private String decodeString(String encoded) {
    byte[] dataDec = Base64.decode(encoded, Base64.DEFAULT);
    String decodedString = "";
    try {

        decodedString = new String(dataDec, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();

    } finally {

        return decodedString;
    }
}
Example
    String text = "example007";

    Log.e("encoded", encodeString(text)); //Output: ZXhhbXBsZTAwNw==
    Log.e("decoded", decodeString(encodeString(text))); //Output: example007

Solution 5 - Java

If you don't want a line break at the end of the String, change the flags from Base64.DEFAULT to Base64.NO_WRAP

Base64.encodeToString("yourString".getBytes("UTF-8"), Base64.NO_WRAP);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionxydevView Question on Stackoverflow
Solution 1 - JavablackpantherView Answer on Stackoverflow
Solution 2 - JavaDan D.View Answer on Stackoverflow
Solution 3 - JavaJared BurrowsView Answer on Stackoverflow
Solution 4 - JavaCommonSenseCodeView Answer on Stackoverflow
Solution 5 - JavaHugo GresseView Answer on Stackoverflow