đź“ť Edit page
âž• Add page
Base64 encoding
Index table
Values 0 to 61 are the following, in order:
A
–Z
a
–z
0
–9
Then the last two values can change but typically +
and /
.
Padding character: =
.
Usage
Browser JavaScript
Use the built-in functions btoa
and atob
. You can use these directly in the browser and also in Node.
Note that b
stands for binary string (UTF-16 encoded strings) and not base-64.
From Mozilla docs:
In JavaScript there are two functions respectively for decoding and encoding base64 strings:
btoa()
: creates a base-64 encoded ASCII string from a “string” of binary data (“btoa” should be read as “binary to ASCII”).atob()
: decodes a base-64 encoded string (“atob” should be read as “ASCII to binary”).
Encode
Convert from binary string to base-64 ASCII string.
> btoa("Hello, world!")
"SGVsbG8sIHdvcmxk"
Decode
> atob("SGVsbG8sIHdvcmxk")
"Hello, world!"
Node
For newer versions of Node you can use the conversions above, otherwise use the Node approach here.
Using the built-in buffer API. The docs cover using an import for Buffer
, however this is not needed.
Encode
const plainText = "hello world";
const base64Encoded = Buffer.from(plainText).toString("base64");
// 'aGVsbG8gd29ybGQ='
Decode
const base64Encoded = 'aGVsbG8gd29ybGQ='
const decodedText = Buffer.from(base64Encoded, "base64").toString("utf-8");
'hello world'
Python
>>> base64.b64decode('SGVsbG8sIHdvcmxkIQ==')
b'Hello, world!'
See Python Base 64 cheatsheet for more info.
Bash shell
- base64 Linux man page.
Use base64
and -d
or --decode
to decode.
Encode
$ echo 'Hello, world!' | base64
SGVsbG8sIHdvcmxkIQo=
Decode
$ echo 'SGVsbG8sIHdvcmxkIQo=' | base64 -d
Hello, world!