๐ŸŒ
Cryptii
cryptii.com โ€บ pipes โ€บ base64-to-hex
Base64 to hex: Encode and decode bytes online - cryptii
Base64 encoding schemes are used when binary data needs to be stored or transferred as textual data. Therefore 64 characters are chosen that are both members of a subset common to most encodings (ASCII), and also printable.
๐ŸŒ
Cryptii
cryptii.com โ€บ pipes โ€บ binary-to-base64
Binary to base64: Convert between bytes and base64 - cryptii
Base64 encoding schemes are used when binary data needs to be stored or transferred as textual data. Therefore 64 characters are chosen that are both members of a subset common to most encodings (ASCII), and also printable.
๐ŸŒ
Code Beautify
codebeautify.org โ€บ binary-to-base64-converter
Binary to base64 Converter
Binary to Base64 Converter Online helps to convert plain Binary to Base64 and helps to save and share Binary with Base64.
๐ŸŒ
Base64Encode.org
base64encode.org โ€บ enc โ€บ array
Base64 Encoding of "array" - Online
Encode array to Base64 format with various advanced options. Our site has an easy to use online tool to convert your data.
๐ŸŒ
Base64 Encode
base64encode.org
Base64 Encode and Decode - Online
Encode to Base64 format or decode from it with various advanced options. Our site has an easy to use online tool to convert your data.
๐ŸŒ
Online Tools
emn178.github.io โ€บ online-tools โ€บ base64_encode.html
Base64 Encode - Online Tools
Encode text or binary to Base64 online using UTF-8, UTF-16, Hex, custom alphabets, Base64URL, MIME, UTF-7, and IMAP variants. Read more ยท Base64 converts binary data into text using a 64-character alphabet. It represents each group of three input bytes as four encoded characters, making binary ...
๐ŸŒ
Base64 Decode
base64decode.org
Base64 Decode and Encode - Online
These three bytes are joined together in a 24 bit buffer producing the binary sequence "010011010110000101101110". Packs of 6 bits (6 bits have a maximum of 64 different binary values) are converted into 4 numbers (24 = 4 * 6 bits) which are then converted to their corresponding values in Base64.
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ dotnet โ€บ api โ€บ system.convert.tobase64string
Convert.ToBase64String Method (System) | Microsoft Learn
... The output length was larger than Int32.MaxValue. ... Converts a subset of an array of 8-bit unsigned integers to its equivalent string representation that is encoded with base-64 digits.
๐ŸŒ
Motobit Software
motobit.com โ€บ util โ€บ base64-decoder-encoder.asp
Base64 Online - base64 decode and encode
This online sample demonstrates functionality of a base64 property, ByteArray class and Huge asp file upload. You can convert texts using several code pages (using CharSet property) from Unicode string to byte array and then convert the binary data to a Base64 string.
Find elsewhere
Top answer
1 of 4
395

Java 8+

Encode or decode byte arrays:

byte[] encoded = Base64.getEncoder().encode("Hello".getBytes());
println(new String(encoded));   // Outputs "SGVsbG8="

byte[] decoded = Base64.getDecoder().decode(encoded);
println(new String(decoded))    // Outputs "Hello"

Or if you just want the strings:

String encoded = Base64.getEncoder().encodeToString("Hello".getBytes());
println(encoded);   // Outputs "SGVsbG8="

String decoded = new String(Base64.getDecoder().decode(encoded.getBytes()));
println(decoded)    // Outputs "Hello"

For more info, see Base64.

Java < 8

Base64 is not bundled with Java versions less than 8. I recommend using Apache Commons Codec.

For direct byte arrays:

Base64 codec = new Base64();
byte[] encoded = codec.encode("Hello".getBytes());
println(new String(encoded));   // Outputs "SGVsbG8="

byte[] decoded = codec.decode(encoded);
println(new String(decoded))    // Outputs "Hello"

Or if you just want the strings:

Base64 codec = new Base64();
String encoded = codec.encodeBase64String("Hello".getBytes());
println(encoded);   // Outputs "SGVsbG8="

String decoded = new String(codec.decodeBase64(encoded));
println(decoded)    // Outputs "Hello"

Android (with Java < 8)

If you are using the Android SDK before Java 8 then your best option is to use the bundled android.util.Base64.

For direct byte arrays:

byte[] encoded = Base64.encode("Hello".getBytes());
println(new String(encoded))    // Outputs "SGVsbG8="

byte [] decoded = Base64.decode(encoded);
println(new String(decoded))    // Outputs "Hello"

Or if you just want the strings:

String encoded = Base64.encodeToString("Hello".getBytes());
println(encoded);   // Outputs "SGVsbG8="

String decoded = new String(Base64.decode(encoded));
println(decoded)    // Outputs "Hello"

Other: Spring < 6.0.5

If for some reason Java's Base64 isn't suitable and you use Spring, see Base64Utils. This package, however, is deprecated as of 6.0.5 and so I don't recommend.

2 of 4
35

Use:

byte[] data = Base64.encode(base64str);

Encoding converts to Base64

You would need to reference commons codec from your project in order for that code to work.

For java8:

import java.util.Base64
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ dotnet โ€บ api โ€บ system.convert.frombase64string
Convert.FromBase64String(String) Method (System) | Microsoft Learn
April 8, 2023 - The following example uses the ToBase64String(Byte[]) method to convert a byte array to a UUencoded (base-64) string, and then calls the FromBase64String(String) method to restore the original byte array.
๐ŸŒ
Base64.Guru
base64.guru โ€บ converter โ€บ decode โ€บ file
Base64 to File | Base64 Decode | Base64 Converter | Base64
Can you please tell us what is the charset of the value displayed in the "Preview" section ? This is because when we try to do the following in Java (JDK 7), the results do not match: - 1) Decode the BASE64 text using javax.xml.bind.DatatypeConverter.parseBase64Binary() which returns a byte array.
Top answer
1 of 13
5

JavaScript, 177 187 198 characters

function(d){c="";for(a=e=b=0;a<4*d.length/3;f=b>>2*(++a&3)&63,c+=String.fromCharCode(f+71-(f<26?6:f<52?0:f<62?75:f^63?90:87)))a&3^3&&(b=b<<8^d[e++]);for(;a++&3;)c+="=";return c}

For adding linebreaks, \r\n, after each 76th character, add 23 characters to the code:

function(d){c="";for(a=e=b=0;a<4*d.length/3;f=b>>2*(++a&3)&63,c+=String.fromCharCode(f+71-(f<26?6:f<52?0:f<62?75:f^63?90:87))+(75==(a-1)%76?"\r\n":""))a&3^3&&(b=b<<8^d[e++]);for(;a++&3;)c+="=";return c}

Demo code:

var encode = function(d,a,e,b,c,f){c="";for(a=e=b=0;a<4*d.length/3;f=b>>2*(++a&3)&63,c+=String.fromCharCode(f+71-(f<26?6:f<52?0:f<62?75:f^63?90:87))+(75==(a-1)%76?"\r\n":""))a&3^3&&(b=b<<8^d[e++]);for(;a++&3;)c+="=";return c};

//OP test case
console.log(encode([99, 97, 102, 195, 169])); // outputs "Y2Fmw6k=".

//Quote from Hobbes' Leviathan:
console.log(
 encode(
  ("Man is distinguished, not only by his reason, but by this singular passion from " +
   "other animals, which is a lust of the mind, that by a perseverance of delight " +
   "in the continued and indefatigable generation of knowledge, exceeds the short " +
   "vehemence of any carnal pleasure.")
  .split('').map(function(i){return i.charCodeAt(0)})
 )
);

2 of 13
4

C, 276 bytes

Quite a bump of this thread but I came up with this question myself a few days ago, then found out about code golfing, then found out about this thread.

Takes the 0-255 int array as first console arg (just a string). Uses K&R C style function parameter declaration and RFC 2045 encoding (except with no 76-character limit).

#define u(_)unsigned _:6;
b(i,j){putchar(j?i>61?43+i%2*4:i+65+6*(i>25)-75*(i>51):61);}i=-3;main(o,c)char**c;{char*s=c[1];for(;(i+=3)<strlen(s);){union{struct{u(a)u(b)u(c)u(d)};char s[3];}a;a.s[2]=s[i];a.s[1]=s[i+1];*a.s=s[i+2];b(a.d);b(a.c);b(a.b,a.s[1]);b(a.a,a.s[1]**a.s);}}

More 'readable' version of the algorithm (it was originally more clear but I started obfusc-golfing it right away so it turned out like this):

#include <stdio.h>
#include <string.h>

void b64_char(int i) {
    putchar(i>61?43+i%2*4:i+65+6*(i>25)-75*(i>51));
}

int i=-3;
int main(int argc, char* argv[]) {
    char* str = argv[1];
    for (;(i+=3)<strlen(str);) {
        union {
            struct {unsigned a:6; unsigned b:6; unsigned c:6; unsigned d:6;};
            char s[3];
        } a;
        a.s[2]=str[i];
        a.s[1]=str[i+1];
        a.s[0]=str[i+2];

        b64_char(a.d);
        b64_char(a.c);
        a.s[1]?b64_char(a.b):putchar(61);
        a.s[0]&&a.s[1]?b64_char(a.a):putchar(61);
    }

    return 0;
}

Defining the Base64 symbol table like that saved me a bunch of bytes. Basically taking advantage of the consecutiveness of letters in ASCII.

Also, architecture dependent since it only works on little endian machines, and fails non-deterministically when the unpassed parameter for the function b(2) happens to be 0. This would have been a lot shorter on a big endian machine.

Can anybody shorten this any further?

๐ŸŒ
Base64.Guru
base64.guru โ€บ home โ€บ base64 converter โ€บ base64 encode
Hex to Base64 | Base64 Encode | Base64 Converter | Base64
"""Convert Hex string to Base64""" import base64 HEX_STRING = '49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d' BYTE_ARRAY = bytearray.fromhex(HEX_STRING) print(BYTE_ARRAY) BASE64_VAL = base64.b64encode(BYTE_ARRAY) print(BASE64_VAL)
๐ŸŒ
Base64 Encode
base64encode.net
Base64 Encode - Online Tool
Encode and Decode Base64 data with this online base64 encoder.
๐ŸŒ
Browserling
browserling.com โ€บ tools โ€บ file-to-base64
File to Base64 Converter - Encode Files to Base64 - Online - Browserling Web Developer Tools
World's simplest online base64 file encoder for web developers and programmers. Just select your file or drag & drop it below, press the Convert to Base64 button, and you'll get a base64 string. Press a button โ€“ get base64.
๐ŸŒ
Teleport
goteleport.com โ€บ home โ€บ resources โ€บ tools โ€บ hex to base64 converter | instant hex to base64 conversion
Hex to Base64 Converter | Instant Hex to Base64 Conversion | Teleport
Base64.getEncoder().encodeToString(bytes) encodes the byte array into a Base64 string. For quick, one-time conversions or when working in environments where running scripts isn't feasible, online hex to Base64 converters offer a fast, user-friendly ...