我一直在玩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
?