What works for me is to change the b64encode line to:

myObj = [base64.b64encode(data).decode('ascii')]

This is explained in https://stackoverflow.com/a/42776711 :

base64 has been intentionally classified as a binary transform.... It was a design decision in Python 3 to force the separation of bytes and text and prohibit implicit transformations.

The accepted answer doesn't work for me (Python 3.9) and gives the error:

Traceback (most recent call last):
  File "/tmp/x.py", line 4, in <module>
    myObj = [base64.b64encode(data)]
  File "/usr/lib64/python3.9/base64.py", line 58, in b64encode
    encoded = binascii.b2a_base64(s, newline=False)
TypeError: a bytes-like object is required, not 'str'
Answer from jmou on Stack Overflow
๐ŸŒ
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.
๐ŸŒ
Base64 Encode
base64encode.org
Base64 Encode and Decode - 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.
๐ŸŒ
Code Beautify
codebeautify.org โ€บ binary-to-base64-converter
Binary to base64 Converter
This tool supports loading the Binary File to transform to Base64.
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.tobase64string
Convert.ToBase64String Method (System) | Microsoft Learn
Dim bytes(arr.Length * 4 - 1) As Byte For ctr As Integer = 0 To arr.Length - 1 Array.Copy(BitConverter.GetBytes(arr(ctr)), 0, bytes, ctr * 4, 4) Next ' Encode the byte array using Base64 encoding Dim base64 As String = Convert.ToBase64String(bytes) Console.WriteLine("The encoded string: ") For ctr As Integer = 0 To base64.Length \ 50 - 1 Console.WriteLine(base64.Substring(ctr * 50, If(ctr * 50 + 50 <= base64.Length, 50, base64.Length - ctr * 50))) Next Console.WriteLine() ' Convert the string back to a byte array.
๐ŸŒ
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.
๐ŸŒ
Quora
quora.com โ€บ How-do-you-convert-bytes-to-Base64-strings
How to convert bytes to Base64 strings - Quora
Answer: The Wikipedia article on Base64 explains this in a lot of detail. A byte is assumed to be 8 bits, i.e. an โ€œoctetโ€. A Base64 value represents \log_2(64)=6 bits โ€” call that a โ€œsextetโ€ by analogy. At its core, Base64 encoding takes 24 bits from three octets and views them as ...
Find elsewhere
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ dotnet โ€บ api โ€บ system.convert.frombase64string
Convert.FromBase64String(String) Method (System) | Microsoft Learn
Dim bytes(arr.Length * 4 - 1) As Byte For ctr As Integer = 0 To arr.Length - 1 Array.Copy(BitConverter.GetBytes(arr(ctr)), 0, bytes, ctr * 4, 4) Next ' Encode the byte array using Base64 encoding Dim base64 As String = Convert.ToBase64String(bytes) Console.WriteLine("The encoded string: ") For ctr As Integer = 0 To base64.Length \ 50 - 1 Console.WriteLine(base64.Substring(ctr * 50, If(ctr * 50 + 50 <= base64.Length, 50, base64.Length - ctr * 50))) Next Console.WriteLine() ' Convert the string back to a byte array.
๐ŸŒ
Medium
tarunjain07.medium.com โ€บ complete-guide-to-data-encoding-from-bits-to-base64-a31b212a0162
Complete Guide to Data Encoding: From Bits to Base64 | by Tarun Jain | Medium
July 25, 2025 - Step 1: Take each byte (8 bits) ... decimal) Hex: 6 | C (C = 12 in hex) Result: 6C ... Since 2โถ = 64, each Base64 character represents exactly 6 bits....
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ base64.html
base64 โ€” Base16, Base32, Base64, Base85 Data Encodings
This module provides functions for encoding binary data to printable ASCII characters and decoding such encodings back to binary data. This includes the encodings specified in RFC 4648 (Base64, Base32 and Base16), the Base85 encoding specified in PDF 2.0, and non-standard variants of Base85 used elsewhere. There are two interfaces provided by this module. The modern interface supports encoding bytes-like objects to ASCII bytes, and decoding bytes-like objects or strings containing ASCII to bytes.
๐ŸŒ
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 ...
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Glossary โ€บ Base64
Base64 - Glossary - MDN Web Docs - Mozilla
This version, defined in RFC 4648, ... parameter โ€” for example, data URLs have neither and can use the standard Base64 encoding. Each Base64 digit represents 6 bits of data (64 = 26). So, three 8-bit bytes of the input string/binary file (3ร—8 bits = 24 bits) can be represented ...
๐ŸŒ
Motobit Software
motobit.com โ€บ util โ€บ base64-decoder-encoder.asp
Base64 Online - base64 decode and encode
The ByteArray class was primarily designed to work with ASP and VBScript, but you can use it with any other language working with COM (ActiveX, OLE) objects, such is VBA (VBA5, VBA6, Word, Excel, MS Access), VBScript and JScript in windows scripting host (.wsh, .chm or .hta applications, Outlook or echange server-side scripts), VB.Net, C# or j# in ASP.Net and others. On-line base64 decoder and encoder - let's you convert text data and files from/to a Base64 string.
๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ Base64
Base64 - Wikipedia
July 30, 2026 - For example, the letter 'M' has the value 77 (per ASCII and similar). The first 6 bits of the value is 010011 or 19 decimal which maps to Base64 letter 'T' which has a value 84 (per ASCII and similar). If the input consists of a number of bytes that is 2 more than a multiple of 3 (e.g.
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?

๐ŸŒ
GitHub
gist.github.com โ€บ llzes โ€บ 24fe0a9b3d786d48f2b3932a633d5b2b
Byte to Base64 implementation in JavaScript ยท GitHub
Byte to Base64 implementation in JavaScript. GitHub Gist: instantly share code, notes, and snippets.
๐ŸŒ
Online String Tools
onlinestringtools.com โ€บ convert-string-to-base64
Base64-encode a String โ€“ Online String Tools
convert bytes to string ยท find string length ยท url-encode a string ยท reverse a string ยท convert string to netstring ยท ANNOUNCEMENT ยท Check out our project Browserling โ€“ anonymous cloud browser. Free online string to base64 encoder. Just load your string and it will automatically get converted to base64.