codec

Encode Base64

Photo of Byron KiourtzoglouByron KiourtzoglouNovember 11th, 2012Last Updated: October 7th, 2013
0 207 1 minute read

In this example we shall show you how to encode 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. To encode a String with Base64 one should perform the following steps:

  • Create a String.
  • Get the bytes from the String, usinggetBytes() API method of String.
  • UseencodeBase64(byte[] binaryData) method oforg.apache.commons.codec.binary.Base64 to encode the byte array using the base64 algorithm.
  • Print the encoded array, using thetoString(byte[] a) API method ofArrays,

as described in the code snippet below.

package com.javacodegeeks.snippets.core; import org.apache.commons.codec.binary.Base64;import java.util.Arrays; public class encodeBase64 {    public static void main(String[] args) {      String string = "Javacodegeeks";   // Get bytes from string  byte[] byteArray = Base64.encodeBase64(string.getBytes());   // Print the encoded byte array  System.out.println(Arrays.toString(byteArray));   // Print the encoded string  String encodedString = new String(byteArray);  System.out.println(string + " = " + encodedString);    }}

Output:

[83, 109, 70, 50, 89, 87, 78, 118, 90, 71, 86, 110, 90, 87, 86, 114, 99, 119, 61, 61]Javacodegeeks = SmF2YWNvZGVnZWVrcw==

 
This was an example of how to encode a String with Base64 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 Byron KiourtzoglouByron KiourtzoglouNovember 11th, 2012Last Updated: October 7th, 2013
0 207 1 minute read
Photo of Byron Kiourtzoglou

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor atJava Code Geeks.

Related Articles

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