codec
Decode Base64
This is an example of how to decode Strings with the Base64 algorithm. We are using theorg.apache.commons.codec.binary.Base64 class that provides Base64 encoding and decoding as defined by RFC 2045. Decoding withorg.apache.commons.codec.binary.Base64 class implies that you should:
- Create a String.
- Get the bytes from the String, using
getBytes()API method of String. - Use
decodeBase64(byte[] base64Data)API method, using the byte array from the String to decode Base64 data into octets. - Print the decoded array, using the
toString(byte[] a)API method ofArrays.
Let’s take a look at the code snippet that follows:
package com.javacodegeeks.snippets.core; import org.apache.commons.codec.binary.Base64; import java.util.Arrays; public class decodeBase64 { public static void main(String[] args) { String string = "SmF2YWNvZGVnZWVrcw=="; // Get bytes from string byte[] byteArray = Base64.decodeBase64(string.getBytes()); // Print the decoded array System.out.println(Arrays.toString(byteArray)); // Print the decoded string String decodedString = new String(byteArray); System.out.println(string + " = " + decodedString); }}Output:
[74, 97, 118, 97, 99, 111, 100, 101, 103, 101, 101, 107, 115]SmF2YWNvZGVnZWVrcw== = Javacodegeeks
This was an example of how to decode Strings with the Base64 algorithm in Java.
Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to theTerms andPrivacy Policy

Thank you!
We will contact you soon.
Ilias Tsagklis
Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor atJava Code Geeks.



