The simplest approach would be: Array to json to base64:

import json
import base64

data = [0, 1, 0, 0, 83, 116, -10]
dataStr = json.dumps(data)

base64EncodedStr = base64.b64encode(dataStr.encode('utf-8'))
print(base64EncodedStr)

print('decoded', base64.b64decode(base64EncodedStr))

Prints out:

>>> WzAsIDEsIDAsIDAsIDgzLCAxMTYsIC0xMF0=
>>> ('decoded', '[0, 1, 0, 0, 83, 116, -10]')  # json.loads here !

... another option could be using bitarray module.

Answer from Maurice Meyer on Stack Overflow
🌐
Finxter
blog.finxter.com › home › learn python blog › 5 best ways to convert python bytes to base64
5 Best Ways to Convert Python Bytes to Base64 - Be on the Right Side of Change
February 23, 2024 - If you’re looking for a concise way to encode bytes to base64 within another piece of code such as within a map or filter operation, a lambda function can be a quick, one-liner solution. ... import base64 data = b'Python bytes to base64' encoded_data = (lambda x: base64.b64encode(x).decode())(data) print(encoded_data)
Discussions

python - How to convert a Base64 file to bytes? - Stack Overflow
I am trying to pass a base64 to bytes. But I think I'm doing it wrong because I'm passing it to ascii. The file is much bigger but I didn't want to put it all. I hope you can support me. def conver... More on stackoverflow.com
🌐 stackoverflow.com
code golf - Convert a bytes array to base64 - Code Golf Stack Exchange
Your mission is to write a function/program that converts an array of bytes (i.e: an array of integers from 0 to 255), to base64. Using built-in base64 encoders is not allowed. The required base64 More on codegolf.stackexchange.com
🌐 codegolf.stackexchange.com
Why does base64.b64encode() in python3 return a bytes object, not a string object? Isn't converting to string the point?
A bytes object is not "a byte array". It is a sequence, the elements of which are byte values. The encoding produces bytes because the original purpose of encoding things that way was to be able to transmit binary data over ASCII protocols that might reserve the high bit of each byte for other uses, randomly insert line breaks, etc. (The expected input is also a bytes object, not a string.) A lot of this is the result of a legacy of pretending that we can treat strings as being made up of bytes. It turns out that this doesn't actually work; a usable conception of a string in the modern world needs to a) be more abstract and b) allow for more than 256 possibilities for each element. But fundamentally everything we store in memory or on disk (or transmit over the Internet) boils down to a sequence of bytes at some level. Encodings (such as ASCII, UTF-8, etc.) determine how to interpret bytes as text. I was confusing bytes with binary. Programmers use the word "binary" in a few related but subtly different ways - be careful. More on reddit.com
🌐 r/learnpython
5
2
July 29, 2017
From hex to base64 without encoding
You need to convert them like this (Python3): import codecs hex = "10000000000002ae" b64 = codecs.encode(codecs.decode(hex, 'hex'), 'base64').decode() I don't know what you mean with "without implementing encodings", but that's how you can convert hex to base64. More on reddit.com
🌐 r/learnpython
4
4
March 2, 2021
🌐
Python
docs.python.org › 3 › library › base64.html
base64 — Base16, Base32, Base64, Base85 Data Encodings
Encode the bytes-like object s using Base64 and return the encoded bytes. Optional altchars must be a bytes-like object of length 2 which specifies an alternative alphabet for the + and / characters.
🌐
MojoAuth
mojoauth.com › binary-encoding-decoding › base64-with-python
Base64 with Python | Binary Encoding Techniques Across Programming Languages
Python's built-in base64 module makes this straightforward, handling the complexities of the encoding process. This guide will walk you through using base64 for common encoding and decoding tasks in Python. You'll learn how to convert various data types, like strings and bytes, into their Base64 ...
🌐
Educative
educative.io › answers › how-to-use-base64b64encode-in-python
How to use base64.b64encode() in Python
The program then encodes the bytes in base64 and specifies in the altchars argument that / or + is encoded with :. To check the correctness of the encoding, the program decodes the encoded data using the base64.b64decode() function.
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › base64-data-encodings-using-python
Cryptography - Base64 Encoding & Decoding
July 30, 2019 - Below is the Python implementation using standard_b64encode() function − · import base64 def base64_encoding(data): encoded_bytes = base64.standard_b64encode(data) encoded_string = encoded_bytes.decode('utf-8') return encoded_string # Our ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › encoding-and-decoding-base64-strings-in-python
Encoding and Decoding Base64 Strings in Python - GeeksforGeeks
August 7, 2024 - It is a type of conversion of bytes to ASCII characters. the list of available Base64 characters are mentioned below: ... Each Base64 character represents 6 bits of data. it is also important to note that it is not meant for encryption for obvious reasons. To convert a string into a Base64 character the following steps should be followed:
🌐
Finxter
blog.finxter.com › home › learn python blog › 5 methods to convert python bytearray to base64
5 Methods to Convert Python Bytearray to base64 - Be on the Right Side of Change
February 24, 2024 - This code snippet takes our example bytearray, encodes it using the base64.b64encode() function, and then decodes it back into a string. This approach is the official recommended method for base64 encoding in Python.
🌐
Quora
quora.com › How-do-you-encode-strings-to-Base64-in-Python
How to encode strings to Base64 in Python - Quora
Answer (1 of 4): Use the base64 module, which is part of the Python standard library. The methods there take “bytes-like objects”, so you will most likely have to create a [code ]bytes[/code] object from your string, specifying which encoding to use. Here’s an example: [code]>>> ...
🌐
Stack Abuse
stackabuse.com › encoding-and-decoding-base64-strings-in-python
Encoding and Decoding Base64 Strings in Python
September 4, 2023 - In the code above, we first imported the base64 module. The message variable stores our input string to be encoded. We convert that to a bytes-like object using the string's encode method and store it in message_bytes. We then Base64 encode message_bytes and store the result in base64_bytes using the base64.b64encode method.
🌐
Python Module of the Week
pymotw.com › 2 › base64
base64 – Encode binary data into ASCII characters - Python Module of the Week
The encoding process looks at each sequence of 24 bits in the input (3 bytes) and encodes those same 24 bits spread over 4 bytes in the output. The last two characters, the ==, are padding because the number of bits in the original string was not evenly divisible by 24 in this example. $ python base64_b64decode.py Original: This is the data, in the clear. Encoded : VGhpcyBpcyB0aGUgZGF0YSwgaW4gdGhlIGNsZWFyLg== Decoded : This is the data, in the clear. Because the default base64 alphabet may use + and /, and those two characters are used in URLs, it became necessary to specify an alternate encoding with substitutes for those characters.
🌐
Educative
educative.io › answers › how-to-use-base64b64decode-in-python
How to use base64.b64decode() in Python
The program then encodes the bytes in base64 and specifies in the altchars argument that / or + is to be encoded with :. To check the correctness of the encoding, the program decodes the encoded data using the base64.b64decode() function.
🌐
Codetinkering
codetinkering.com › python-base64-strings-bytes
Base64 shenanigans in Python – Code Tinkering
November 30, 2022 - # This can be set as ascii, uti-8, utf-16, utf-32 my_bytes = my_string.encode('utf-8') # Next, use the base64 encode method to encode the bytes to base64 base64_bytes = base64.b64encode(my_bytes) # Finally, you can convert the bytes to a string again if needed: base64_string = str(base64_bytes) ...
🌐
Finxter
blog.finxter.com › 5-best-ways-to-convert-python-bytes-object-to-base64
5 Best Ways to Convert Python Bytes Object to Base64 – Be on the Right Side of Change
... This code snippet first imports the base64 module and then creates a bytes object containing the string “hello world”. It then calls the base64.b64encode() function to convert the bytes object to a Base64 encoded bytes object, which is printed to the console.
🌐
Python
docs.python.org › 3.1 › library › base64.html
18.6. base64 — RFC 3548: Base16, Base32, Base64 Data Encodings — Python v3.1.5 documentation
Encode a byte string using Base64. s is the string to encode. Optional altchars must be a string of at least length 2 (additional characters are ignored) which specifies an alternative alphabet for the + and / characters. This allows an application to e.g. generate URL or filesystem safe Base64 ...
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?

🌐
Python Guides
pythonguides.com › convert-string-to-base64-in-python
How to Convert String to Base64 in Python (5 Simple Methods)
April 22, 2025 - Read How to Convert a String to Lowercase in Python? When you need to perform base64 encoding frequently throughout your codebase, it’s a good practice to create a reusable function: import base64 def string_to_base64(input_string, url_safe=False): """ Convert a string to base64 encoded string. Args: input_string (str): String to encode url_safe (bool): Whether to use URL-safe encoding Returns: str: Base64 encoded string """ string_bytes = input_string.encode('utf-8') if url_safe: encoded_bytes = base64.urlsafe_b64encode(string_bytes) else: encoded_bytes = base64.b64encode(string_bytes) retu
🌐
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 ...