Java Base64 编码器和解码器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4322182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:51:35  来源:igfitidea点击:

Base64 encoder and decoder

javaandroidbase64encoderdecoder

提问by xydev

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

Android 中是否有用于字符串的 base-64 解码器和编码器?

采纳答案by Dan D.

See android.util.Base64

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.

似乎这是在 API 版本 8 或 android 2.2 中添加的,因此在旧平台上将不可用。

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

但是它的来源在android/util/Base64.java所以如果需要,可以将它复制到旧版本不变。

回答by blackpanther

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

这是一个如何使用Base64该类对简单字符串值进行编码和解码的示例。

// 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.

此摘录可以包含在 Android 活动中。

回答by Jared Burrows

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

这是我打算使用的一个简单方法,直到我意识到这仅在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);
}

回答by Hugo Gresse

If you don'twant a line breakat the end of the String, change the flags from Base64.DEFAULTto Base64.NO_WRAP

如果你想要一个换行符在字符串的结尾,标志从改变Base64.DEFAULTBase64.NO_WRAP

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

回答by CommonSenseCode

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