Encoding and Decoding Strings Using Base64
While attempting to encode and decode a string using Base64, you may encounter difficulties. The provided code can be optimized to ensure successful encoding and decoding.
Encoding a String:
Decoding a String:
Code Sample:
// Encoding String plaintext = "password"; byte[] bytes = plaintext.getBytes(StandardCharsets.UTF_8); String base64Encoded = Base64.encodeToString(bytes, Base64.DEFAULT); // Decoding byte[] decodedBytes = Base64.decode(base64Encoded, Base64.DEFAULT); String decodedText = new String(decodedBytes, StandardCharsets.UTF_8);
Additional Notes:
The above is the detailed content of How Can I Efficiently Encode and Decode Strings Using Base64?. For more information, please follow other related articles on the PHP Chinese website!