Convert Byte Array to Base64 String in Java

Convert byte array to base64 String in java

In this article, we will see how to convert byte array to base64 String in java.

Using Base64 to Convert Byte Array to Base64 String in Java [ Java 8+]

Java 8 has finally introduced Base64 functionalities via java.util.Base64 class.

You can use Base64's encode() method to convert Byte Array to Base64 String in Java. It encodes input without any line separation.
Here is simple code:

package org.arpit.java2blog;

import java.util.Base64;

public class ConvertByteArrayToBase64String {
    public static void main(String[] args) {
        // Java 8
        // Using encode()
        byte[] encode = Base64.getEncoder().encode("Java2blog".getBytes());
        String result = new String(encode);
        System.out.println(result);

        // Using encodeToString() to get String directly
        String encodeToString = Base64.getEncoder().encodeToString("Java2blog".getBytes());
        System.out.println(encodeToString);
    }
}

Output:

SmF2YTJibG9n
SmF2YTJibG9n

In base64, length of encoded String should be multiple of 3. Sometimes, encode appends one or two padding characters to make it multiple of 3.

In case, you don’t want to append extra padding characters, you can skip padding using withPadding() method. This is useful when you already know that you are not going to decode this String back.
Here is the code:

 String encodeToString = Base64.getEncoder().withoutPadding().encodeToString("Java2blog".getBytes());

Using Apache Common Codec to Convert Byte Array to Base64 String in Java [ < Java 8]

If you are using Java 7 or below version. there is no direct utility class to convert Byte Array to Base64 String in Java.

You can use Apache common codec’s Base64 class to achieve the same.
You need to add following dependenices in your pom.xml.

  
            commons-codec
            commons-codec
            1.15
        
package org.arpit.java2blog;

import org.apache.commons.codec.binary.Base64;

public class ConvertByteArrayToBase64StringApache {

    public static void main(String[] args) {

        Base64 codec = new Base64();
        // Using encode()
        byte[] encode = codec.encode("Java2blog".getBytes());
        String result = new String(encode);
        System.out.println(result);

        // Using encodeBase64String() to get String directly
        Base64 base64Codec = new Base64();
        String base64String = base64Codec.encodeBase64String("Java2blog".getBytes());
        System.out.println(base64String);
    }
}

Output:

SmF2YTJibG9n
SmF2YTJibG9n

Using Base64Utils to Convert Byte Array to Base64 String in Spring

if you are using Spring in your project, you can use Base64Utils to convert Byte Array to Base64 String in java. It provides exact same functions as java.util.Base64.
You need to add following dependenices in your pom.xml.

  
            org.springframework
            spring-core
            5.1.10.RELEASE
   
package org.arpit.java2blog;

import org.springframework.util.Base64Utils;

public class ConvertByteArrayToBase64StringSpring {
    public static void main(String[] args) {
        // Spring
        // Using encode()
        byte[] encode = Base64Utils.encode("Java2blog".getBytes());
        String result = new String(encode);
        System.out.println(result);

        // Using encodeToString() to get String directly
        String encodeToString = Base64Utils.encodeToString("Java2blog".getBytes());
        System.out.println(encodeToString);
    }
}

Output:

SmF2YTJibG9n
SmF2YTJibG9n

Using android.util.Base64 to Convert Byte Array to Base64 String in Android

if you are using android in your project, you can use android.util.Base64 to convert Byte Array to Base64 String in java. It provides exact same functions as java.util.Base64.

package org.arpit.java2blog;

import android.util.Base64;

public class ConvertByteArrayToBase64StringAndroid {
    public static void main(String[] args) {
        // Android
        // Using encode()
        byte[] encode = Base64.getEncoder().encode("Java2blog".getBytes());
        String result = new String(encode);
        System.out.println(result);

        // Using encodeToString() to get String directly
        String encodeToString = Base64.getEncoder().encodeToString("Java2blog".getBytes());
        System.out.println(encodeToString);
    }
}

Output:

SmF2YTJibG9n
SmF2YTJibG9n

That’s all about how to convert Byte Array to Base64 String in java.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.