如何将Base64解码为原始字符串?

时间:2015-06-11 22:37:09

标签: java string encoding base64 decoding

我一直在玩Java并阅读有关Base64编码和解码的一些帖子。我编写了一个小应用程序,将字符串编码为Base64,然后当我解码它时,是否应该将其解码回原始字符串(我是java的新类型,所以如果这是一个愚蠢的问题,请给我一些松弛) ?这是我的代码

import java.util.Base64;

public class Main {
    public static void main(String... args) throws Exception {

        String originalText = "Hello, World!";
        byte[] original = "Hello, World!".getBytes("UTF-8");
        String encoded = Base64.getEncoder().encodeToString(original);
        byte[] decoded = Base64.getDecoder().decode(encoded);

        System.out.println(originalText + " Encoded => " + encoded);
        System.out.println(encoded + " Decoded => " + decoded);

    }
}

编辑后,我得到了这个

import java.util.Base64;

public class Main {
    public static void main(String... args) throws Exception {

        String originalText = "Hello, World!";
        byte[] original = "Hello, World!".getBytes("UTF-8");
        String encoded = Base64.getEncoder().encodeToString(original);
        byte[] decoded = Base64.getDecoder().decode(encoded);

        String byteString = new String(decoded);

        System.out.println(originalText + " Encoded => " + encoded);
        System.out.println(encoded + " Decoded => " + byteString);

    }
}

所以,基本上不是让第二个Print语句打印encoded + "Decoded => " + decoded,我只需将decoded切换为byteString

0 个答案:

没有答案