Home » Core Java » apache » commons » codec » Decode Base64

About Ilias Tsagklis

Avatar photo
Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.

Decode Base64

This is an example of how to decode Strings with the Base64 algorithm. We are using the org.apache.commons.codec.binary.Base64 class that provides Base64 encoding and decoding as defined by RFC 2045. Decoding with org.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 of Arrays.

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 a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

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 ....

 

Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions

 

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments