Home | Mirror | Search

5. Java Base64

import java.io.*;
public class base64Test {

	public static void main(String[] args) {

	    try {
	        String text = "This is an encoded string";
	        //Convert a string to base64 string
	        byte[] buf = text.getBytes();
	        String encode = new sun.misc.BASE64Encoder().encode(buf);
	        System.out.println(encode);

	        // Convert base64 string to a string
	        buf = new sun.misc.BASE64Decoder().decodeBuffer(encode);
	        String decode = new String(buf);
	        System.out.println(decode);
	    } catch (IOException e) {

	    }
	}
}
		
comments powered by Disqus