Monday, July 01, 2013

Convert Base64 to Byte and Byte to Base64 in Java


Simple Ready to use utility class to convert : base64 -> byte and byte -> base64
import org.apache.commons.codec.binary.Base64;


public class Util {

 /**
  * 
  * This method take base64 type string as input and
  * convert it to byte[] array
  *  
  * @param data base64 type string
  * @return byte array
  * @throws Exception
  *  
  */

 @SuppressWarnings("unused")
 public static byte[] base64ToByte(String data) throws Exception {
  Base64 base64 = new Base64();
  return Base64.decodeBase64(data);
 }

 /**
  * This method take byte array as input and
  * convert it to base64 string
  * 
  * @param data byte array
  * @return base64 string
  * 
  */
 @SuppressWarnings("static-access")
 public static String byteToBase64(byte[] data) {
  Base64 base64 = new Base64();
  return base64.encodeBase64String(data);
 }

}

1 comment: