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
๐ŸŒ
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.
Discussions

python - Convert byte string to base64-encoded string (output not being a byte string) - Stack Overflow
I was wondering if it is possible to convert a byte string which I got from reading a file to a string (so type(output) == str). All I've found on Google so far has been answers like How do you bas... More on stackoverflow.com
๐ŸŒ stackoverflow.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
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
Python Base64 Encode
You need to get the bytes of the hash using an encoding. MSFT tends to use Unicode (usually utf16le) and then converts those bytes to base64 It's common to see this: Convert.ToBase64String(Text.Encoding.Unicode.GetBytes("string")); More on reddit.com
๐ŸŒ r/learnpython
2
7
May 20, 2018
๐ŸŒ
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 - The binascii.b2a_base64() function is used in this method, providing a direct way to base64-encode binary data. ... import binascii data = b'Python bytes to base64' encoded_data = binascii.b2a_base64(data) print(encoded_data.decode().strip())
๐ŸŒ
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 displays the encoded data. To check the correctness of the encoding, the program decodes the encoded data using the base64.b64decode() function.
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ encoding-and-decoding-base64-strings-in-python
Encoding and Decoding Base64 Strings in Python
September 4, 2023 - Without using 'rb', Python would assume we are reading a text file. We then use the read() method to get all the data in the file into the binary_file_data variable. Similar to how we treated strings, we Base64 encoded the bytes with base64.b64encode and then used the decode('utf-8') on base64_encoded_data to get the Base64 encoded data using human-readable characters.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ encoding-and-decoding-base64-strings-in-python
Encoding and Decoding Base64 Strings in Python - GeeksforGeeks
August 7, 2024 - In Python the base64 module is used to encode and decode data. First, the strings are converted into byte-like objects and then encoded using the base64 module.
Find elsewhere
๐ŸŒ
MojoAuth
mojoauth.com โ€บ binary-encoding-decoding โ€บ base64-with-python
Base64 with Python | Binary Encoding Techniques Across Programming Languages
This process guarantees your text data is correctly represented for Base64 transformation. To convert Base64 encoded data back into its original form, Python's base64 module provides the b64decode() function.
๐ŸŒ
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]>>> ...
๐ŸŒ
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 ...
๐ŸŒ
Finxter
blog.finxter.com โ€บ 5-methods-to-convert-python-bytearray-to-base64
5 Methods to Convert Python Bytearray to base64 โ€“ Be on the Right Side of Change
The base64.b64encode() function takes a bytes-like object and returns its base64 encoded version as a bytes object, which can be further decoded to a string if necessary. ... import base64 bytearr = bytearray('Python bytearray to base64', 'utf-8') base64_encoded = base64.b64encode(bytearr) ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ encoding-and-decoding-base64-strings-in-python
Cryptography - Base64 Encoding & Decoding
August 7, 2023 - To encode the message, we will use the method base64.b64encode(). This function converts the data into a format that is safe for transmission over systems that handle only text. It is a simple example for understanding the concept of Base64 encoding.
๐ŸŒ
Python Engineer
python-engineer.com โ€บ posts โ€บ base64-module
The base64 Module in Python - Python Engineer
June 21, 2022 - The base64 module is used to encode and decode the data in the following ways: ... The base64 module provides the b64encode() function. It encodes a bytes-like object using Base64 and returns the encoded bytes.
๐ŸŒ
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.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ why does base64.b64encode() in python3 return a bytes object, not a string object? isn't converting to string the point?
r/learnpython on Reddit: Why does base64.b64encode() in python3 return a bytes object, not a string object? Isn't converting to string the point?
July 29, 2017 -

The question is pretty much in the title. I was working on porting some code to Python3 that talks to an old API at work - there is a bunch of base64 encoding and hmac creation, which when going from Python 2 to 3 means being explicit about what is a string and what is a byte array. But the b64encode returning a byte array tripped me up, because it doesn't make sense (unless I'm missing something).

Shouldn't b64encode take bytes and return string, and b64decode take string and return bytes?

๐ŸŒ
Medium
medium.com โ€บ @ume.cooray โ€บ understanding-base64-encoding-in-python-5b31f23dd007
Understanding Base64 Encoding and Decoding in Python | by Umayangana Cooray | Medium
March 18, 2025 - In Python, strings need to be converted to bytes before encoding. This is done using .encode('utf-8'). This ensures the string is represented properly in binary form. ... base64.b64encode() takes the byte data and encodes it into Base64.
๐ŸŒ
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 displays the encoded data. To check the correctness of the encoding, the program decodes the encoded data using the base64.b64decode() function.
๐ŸŒ
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 ...
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ python-base64-encode
Python Base64 Encode | b64encode() Function Guide
February 1, 2024 - We then define a byte string data that we want to encode. We pass this data to the b64encode() function, which returns the base64 encoded version of the data. This encoded data is then printed out.