@Test public void testEncodeToString() { assertEquals("", Base64.encodeToString(new byte[] {})); assertEquals("Qg==", Base64.encodeToString(new byte[] {0x42})); assertEquals("AQID", Base64.encodeToString(new byte[] {1, 2, 3})); assertEquals("CAIGAP8B/wA=", Base64.encodeToString(new byte[] {8, 2, 6, 0, -1, 1, -1, 0})); }
public static String encodeToString(byte[] bytes) { int paddingCount = (3 - (bytes.length % 3)) % 3; byte[] padded = new byte[bytes.length + paddingCount]; System.arraycopy(bytes, 0, padded, 0, bytes.length); StringBuilder sb = new StringBuilder(); for (int i = 0; i < bytes.length; i += 3) { int j = ((padded[i] & 0xff) << 16) + ((padded[i + 1] & 0xff) << 8) + (padded[i + 2] & 0xff); sb.append(ALPHABET.charAt((j >> 18) & 0x3f)).append(ALPHABET.charAt((j >> 12) & 0x3f)) .append(ALPHABET.charAt((j >> 6) & 0x3f)).append(ALPHABET.charAt(j & 0x3f)); } return sb.substring(0, sb.length() - paddingCount) + PADDING.substring(0, paddingCount); }
Base64 { public static String encodeToString(byte[] bytes) { int paddingCount = (3 - (bytes.length % 3)) % 3; byte[] padded = new byte[bytes.length + paddingCount]; System.arraycopy(bytes, 0, padded, 0, bytes.length); StringBuilder sb = new StringBuilder(); for (int i = 0; i < bytes.length; i += 3) { int j = ((padded[i] & 0xff) << 16) + ((padded[i + 1] & 0xff) << 8) + (padded[i + 2] & 0xff); sb.append(ALPHABET.charAt((j >> 18) & 0x3f)).append(ALPHABET.charAt((j >> 12) & 0x3f)) .append(ALPHABET.charAt((j >> 6) & 0x3f)).append(ALPHABET.charAt(j & 0x3f)); } return sb.substring(0, sb.length() - paddingCount) + PADDING.substring(0, paddingCount); } }
Base64 { public static String encodeToString(byte[] bytes) { int paddingCount = (3 - (bytes.length % 3)) % 3; byte[] padded = new byte[bytes.length + paddingCount]; System.arraycopy(bytes, 0, padded, 0, bytes.length); StringBuilder sb = new StringBuilder(); for (int i = 0; i < bytes.length; i += 3) { int j = ((padded[i] & 0xff) << 16) + ((padded[i + 1] & 0xff) << 8) + (padded[i + 2] & 0xff); sb.append(ALPHABET.charAt((j >> 18) & 0x3f)).append(ALPHABET.charAt((j >> 12) & 0x3f)) .append(ALPHABET.charAt((j >> 6) & 0x3f)).append(ALPHABET.charAt(j & 0x3f)); } return sb.substring(0, sb.length() - paddingCount) + PADDING.substring(0, paddingCount); } private  Base64(); }
Base64 { public static String encodeToString(byte[] bytes) { int paddingCount = (3 - (bytes.length % 3)) % 3; byte[] padded = new byte[bytes.length + paddingCount]; System.arraycopy(bytes, 0, padded, 0, bytes.length); StringBuilder sb = new StringBuilder(); for (int i = 0; i < bytes.length; i += 3) { int j = ((padded[i] & 0xff) << 16) + ((padded[i + 1] & 0xff) << 8) + (padded[i + 2] & 0xff); sb.append(ALPHABET.charAt((j >> 18) & 0x3f)).append(ALPHABET.charAt((j >> 12) & 0x3f)) .append(ALPHABET.charAt((j >> 6) & 0x3f)).append(ALPHABET.charAt(j & 0x3f)); } return sb.substring(0, sb.length() - paddingCount) + PADDING.substring(0, paddingCount); } private  Base64(); static String encodeToString(byte[] bytes); }
Base64 { public static String encodeToString(byte[] bytes) { int paddingCount = (3 - (bytes.length % 3)) % 3; byte[] padded = new byte[bytes.length + paddingCount]; System.arraycopy(bytes, 0, padded, 0, bytes.length); StringBuilder sb = new StringBuilder(); for (int i = 0; i < bytes.length; i += 3) { int j = ((padded[i] & 0xff) << 16) + ((padded[i + 1] & 0xff) << 8) + (padded[i + 2] & 0xff); sb.append(ALPHABET.charAt((j >> 18) & 0x3f)).append(ALPHABET.charAt((j >> 12) & 0x3f)) .append(ALPHABET.charAt((j >> 6) & 0x3f)).append(ALPHABET.charAt(j & 0x3f)); } return sb.substring(0, sb.length() - paddingCount) + PADDING.substring(0, paddingCount); } private  Base64(); static String encodeToString(byte[] bytes);  }