Here you can find the source of toBase64(final byte[] value)
Parameter | Description |
---|---|
value | Byte array to convert. |
public static String toBase64(final byte[] value)
//package com.java2s; // and/or modify it under the terms of the GNU General Public License public class Main { private static final char[] BASE_64_ARRAY = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', '=' }; /**/* www. j av a 2s . c o m*/ * Convert byte array to Base64 string. * * @param value * Byte array to convert. * @return Base64 string. */ public static String toBase64(final byte[] value) { StringBuilder str = new StringBuilder((value.length * 4) / 3); int b; for (int pos = 0; pos < value.length; pos += 3) { b = (value[pos] & 0xFC) >> 2; str.append(BASE_64_ARRAY[b]); b = (value[pos] & 0x03) << 4; if (pos + 1 < value.length) { b |= (value[pos + 1] & 0xF0) >> 4; str.append(BASE_64_ARRAY[b]); b = (value[pos + 1] & 0x0F) << 2; if (pos + 2 < value.length) { b |= (value[pos + 2] & 0xC0) >> 6; str.append(BASE_64_ARRAY[b]); b = value[pos + 2] & 0x3F; str.append(BASE_64_ARRAY[b]); } else { str.append(BASE_64_ARRAY[b]); str.append('='); } } else { str.append(BASE_64_ARRAY[b]); str.append("=="); } } return str.toString(); } }