Java Utililty Methods Base64 Convert to

List of utility methods to do Base64 Convert to

Description

The list of methods to do Base64 Convert to are organized into topic(s).

Method

byte[]fromBase64(String data)
Decode a base64 string into a byte array.
if (data == null) {
    return null;
int len = data.length();
assert (len % 4) == 0;
if (len == 0) {
    return new byte[0];
char[] chars = new char[len];
data.getChars(0, len, chars, 0);
int olen = 3 * (len / 4);
if (chars[len - 2] == '=') {
    --olen;
if (chars[len - 1] == '=') {
    --olen;
byte[] bytes = new byte[olen];
int iidx = 0;
int oidx = 0;
while (iidx < len) {
    int c0 = base64Values[chars[iidx++] & 0xff];
    int c1 = base64Values[chars[iidx++] & 0xff];
    int c2 = base64Values[chars[iidx++] & 0xff];
    int c3 = base64Values[chars[iidx++] & 0xff];
    int c24 = (c0 << 18) | (c1 << 12) | (c2 << 6) | c3;
    bytes[oidx++] = (byte) (c24 >> 16);
    if (oidx == olen) {
        break;
    bytes[oidx++] = (byte) (c24 >> 8);
    if (oidx == olen) {
        break;
    bytes[oidx++] = (byte) c24;
return bytes;
longfromBase64(String s)
Convert a string representation of a base64 number into a long.
return fromBase64(s, 0, s.length() - 1);
byte[]fromBase64(String str)

The inverse function of the above.

Converts a string representing the encoding of some bytes in Base-64 to their original form.

int len = str.length();
if (len == 0) {
    throw new NumberFormatException("Empty string");
byte[] a = new byte[len + 1];
int i, j;
for (i = 0; i < len; i++) {
    try {
...
byte[]fromBase64decodedImageToByte(String base64encoded)
from Basedecoded Image To Byte
if (base64encoded == null || base64encoded.trim().length() == 0) {
    return null;
String[] split = base64encoded.split(",");
if (split.length < 2)
    return null;
String base64Image = split[1];
return Base64.getDecoder().decode(base64Image);
...
byte[]fromBase64Impl(String data)
Decode a base64 string into a byte array.
if (data == null) {
    return null;
int len = data.length();
assert (len % 4) == 0;
if (len == 0) {
    return new byte[0];
char[] chars = new char[len];
data.getChars(0, len, chars, 0);
int olen = 3 * (len / 4);
if (chars[len - 2] == '=') {
    --olen;
if (chars[len - 1] == '=') {
    --olen;
byte[] bytes = new byte[olen];
int iidx = 0;
int oidx = 0;
while (iidx < len) {
    int c0 = base64Values[chars[iidx++] & 0xff];
    int c1 = base64Values[chars[iidx++] & 0xff];
    int c2 = base64Values[chars[iidx++] & 0xff];
    int c3 = base64Values[chars[iidx++] & 0xff];
    int c24 = (c0 << 18) | (c1 << 12) | (c2 << 6) | c3;
    bytes[oidx++] = (byte) (c24 >> 16);
    if (oidx == olen) {
        break;
    bytes[oidx++] = (byte) (c24 >> 8);
    if (oidx == olen) {
        break;
    bytes[oidx++] = (byte) c24;
return bytes;
byte[]fromBase64String(String base64)
from Base String
return Base64.getDecoder().decode(base64);
byte[]fromBase64String(String encoded)
Converts Base64 encoded string to byte array.
return Base64.getDecoder().decode(encoded);
byte[]fromBase64Url(String data)
from Base Url
String base64Data = data.replace('-', '+').replace('_', '/');
switch (base64Data.length() % 4) {
case 0:
    break;
case 2:
    base64Data += "==";
    break;
case 3:
...