Base64 String to byte[] in java

We Are Going To Discuss About Base64 String to byte[] in java . So lets Start this Java Article.

Base64 String to byte[] in java

  1. Base64 String to byte[] in java

    You can use java.util.Base64 package to decode the String to byte[].
    Below code which I have used for encode and decode.

  2. Base64 String to byte[] in java

    You can use java.util.Base64 package to decode the String to byte[].
    Below code which I have used for encode and decode.

Solution 1

You can use java.util.Base64 package to decode the String to byte[].
Below code which I have used for encode and decode.

For Java 8 :

import java.io.UnsupportedEncodingException;
import java.util.Base64;

public class Example {

    public static void main(String[] args) {
        try {
            byte[] name = Base64.getEncoder().encode("hello World".getBytes());
            byte[] decodedString = Base64.getDecoder().decode(new String(name).getBytes("UTF-8"));
            System.out.println(new String(decodedString));
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

For Java 6 :

import java.io.UnsupportedEncodingException;
import org.apache.commons.codec.binary.Base64;

public class Main {

    public static void main(String[] args) {
        try {
            byte[] name = Base64.encodeBase64("hello World".getBytes());
            byte[] decodedString = Base64.decodeBase64(new String(name).getBytes("UTF-8"));
            System.out.println(new String(decodedString));
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Original Author Ravi Koradia Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment