codec

Decode Base64

Photo of Ilias TsagklisIlias TsagklisNovember 11th, 2012Last Updated: October 7th, 2013
0 601 1 minute read

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, usinggetBytes() API method of String.
  • UsedecodeBase64(byte[] base64Data) API method, using the byte array from the String to decode Base64 data into octets.
  • Print the decoded array, using thetoString(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.

Photo of Ilias TsagklisIlias TsagklisNovember 11th, 2012Last Updated: October 7th, 2013
0 601 1 minute read
Photo of Ilias Tsagklis

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor atJava Code Geeks.

Related Articles

Encode Base64

November 11th, 2012
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.