Encode To Base64 : Base64 « Date Type « Android






Encode To Base64

  
//package pl.przemelek.android.util;

class Util {
  private static String charSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
   
  public static String encodeToBase64(String text) {  
    int[] encoded = new int[text.length()*8/6+4];
    int trueLen = text.length()*8/6+1;
    int newLen = 0;
    for (int idx=0; idx<text.length(); idx+=3) {
      int end = idx+3;
      if (end>text.length()) {
        end = text.length();
      }
      String toWork = text.substring(idx,end);
      char a = 0;
      char b = 0;
      char c = 0;
      if (toWork.length()>0) { a = toWork.charAt(0); }
      if (toWork.length()>1) { b = toWork.charAt(1); }
      if (toWork.length()>2) { c = toWork.charAt(2); }
      int _a = (a & (63 << 2)) >> 2;      
      int _b = ((a & 3)  << 4) +((b & (15 <<4)) >> 4);
      int _c = ((b & 15) << 2) +((c & (3  <<6)) >> 6);
      int _d = (c & 63);
      encoded[newLen++] = charSet.charAt(_a);
      encoded[newLen++] = charSet.charAt(_b);
      encoded[newLen++] = charSet.charAt(_c);
      encoded[newLen++] = charSet.charAt(_d);      
    }
    String encodedStr = "";
    for (int idx=0; idx<trueLen; idx++) {
      if (encoded[idx]!=0) {
        encodedStr+=(char)encoded[idx];
      }
    }
    while (encodedStr.length()%4!=0) {
      encodedStr+="=";
    }
    return encodedStr;
  }
}

   
    
  








Related examples in the same category

1.Base64 encoding
2.Class Base64 can be used for base64-encoding a byte array and/or for base64-decoding a base64-string.
3.A Base64 Encoder/Decoder.
4.Utilities for encoding and decoding the Base64 representation of binary data.
5.An InputStream that does Base64 decoding on the data read through it.
6.A very fast and memory efficient class to encode and decode to and from BASE64 in full accordance with RFC 2045.
7.Base 64 encode 2