Buffers can be used for taking a string or piece of data and doing Base64 encoding of the result. For example:

> console.log(Buffer.from("Hello World").toString('base64'));
SGVsbG8gV29ybGQ=
> console.log(Buffer.from("SGVsbG8gV29ybGQ=", 'base64').toString('ascii'))
Hello World

The Buffer constructor is a global object, so no require is needed. Buffers created with strings can take an optional encoding parameter to specify what encoding the string is in. The available toString and Buffer constructor encodings are as follows:

'ascii' - for 7 bit ASCII data only. This encoding method is very fast, and will strip the high bit if set.

'utf8' - Multi byte encoded Unicode characters. Many web pages and other document formats use UTF-8.

'ucs2' - 2-bytes, little endian encoded Unicode characters. It can encode only BMP(Basic Multilingual Plane, U+0000 - U+FFFF).

'base64' - Base64 string encoding.

'binary' - A way of encoding raw binary data into strings by using only the first 8 bits of each character. This encoding method is deprecated and should be avoided in favor of Buffer objects where possible. This encoding will be removed in future versions of Node.

Answer from onteria_ on Stack Overflow
🌐
Base64Encode.org
base64encode.org › enc › buffer
Base64 Encoding of "buffer" - Online
Other variations, usually derived from Base64, share this property but differ in the symbols chosen for the last two values; an example is the URL and filename safe "RFC 4648 / Base64URL" variant, which uses "-" and "_". ... In the above quote the encoded value of Man is TWFu. Encoded in ASCII, the letters "M", "a", and "n" are stored as the bytes 77, 97, 110, which are equivalent to "01001101", "01100001", and "01101110" in base-2. These three bytes are joined together in a 24 bit buffer producing the binary sequence "010011010110000101101110".
Top answer
1 of 8
2832

Buffers can be used for taking a string or piece of data and doing Base64 encoding of the result. For example:

> console.log(Buffer.from("Hello World").toString('base64'));
SGVsbG8gV29ybGQ=
> console.log(Buffer.from("SGVsbG8gV29ybGQ=", 'base64').toString('ascii'))
Hello World

The Buffer constructor is a global object, so no require is needed. Buffers created with strings can take an optional encoding parameter to specify what encoding the string is in. The available toString and Buffer constructor encodings are as follows:

'ascii' - for 7 bit ASCII data only. This encoding method is very fast, and will strip the high bit if set.

'utf8' - Multi byte encoded Unicode characters. Many web pages and other document formats use UTF-8.

'ucs2' - 2-bytes, little endian encoded Unicode characters. It can encode only BMP(Basic Multilingual Plane, U+0000 - U+FFFF).

'base64' - Base64 string encoding.

'binary' - A way of encoding raw binary data into strings by using only the first 8 bits of each character. This encoding method is deprecated and should be avoided in favor of Buffer objects where possible. This encoding will be removed in future versions of Node.

2 of 8
402

The accepted answer previously contained new Buffer(), which is considered a security issue in Node.js versions greater than 6 (although it seems likely for this use case that the input can always be coerced to a string).

The Buffer constructor is deprecated according to the documentation.

The code snippets should read:

console.log(Buffer.from("Hello World").toString('base64'));
console.log(Buffer.from("SGVsbG8gV29ybGQ=", 'base64').toString('ascii'));

After this answer was written, it has been updated and now matches this.

Discussions

How can I convert a base64 string to ArrayBuffer?
You get access to the array buffer through the .buffer property of what is returned from Uint8Array 2018-02-07T19:22:04.97Z+00:00 ... @Saites, There's nothing wrong with atob or btoa, you just have to give them valid input. atob needs a valid base64 string, otherwise it will throw an error. More on stackoverflow.com
🌐 stackoverflow.com
How can I convert an ArrayBuffer to a base64-encoded string?
This is the correct answer when btoa or Buffer are not available (react-native) 2021-05-14T18:42:25.307Z+00:00 ... Save this answer. ... Show activity on this post. Below are 2 simple functions for converting Uint8Array to Base64 String and back again More on stackoverflow.com
🌐 stackoverflow.com
Buffer to base64 | Node.js
I have a buffer which is being populated with a bunch of base64 strings. I want a way to decode those string and be able to read all of the base64 strings in that buffer. const slugId1 = ' More on stackoverflow.com
🌐 stackoverflow.com
Reading from buffer to base64
I'm trying to store images in base64 format in a db. I tried storing the base64 encoded versions of the images and reading them back, but I don't get the full picture back as some parts of it get corrupted for some reason. ... Why do you store images as base64 strings? Why not as blob? For debugging, I would remove the database from the equation and test encoding and decoding step by step. ... Error when decoding base64 with buffer... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Better Stack
betterstack.com › community › questions › how-to-do-base64-encoding-in-node-js
How can I do Base64 encoding in Node.js? | Better Stack Community
March 11, 2024 - The Buffer.from(originalString) method creates a Buffer object from the original string. The toString('base64') method converts the Buffer object to a Base64-encoded string.
🌐
Node.js
nodejs.org › api › buffer.html
Buffer | Node.js v26.10.0 Documentation
import { Buffer } from 'node:buffer'; const buf = Buffer.from('hello world', 'utf8'); console.log(buf.toString('hex')); // Prints: 68656c6c6f20776f726c64 console.log(buf.toString('base64')); // Prints: aGVsbG8gd29ybGQ= console.log(Buffer.from('fhqwhgads', 'utf8')); // Prints: <Buffer 66 68 71 77 68 67 61 64 73> console.log(Buffer.from('fhqwhgads', 'utf16le')); // Prints: <Buffer 66 00 68 00 71 00 77 00 68 00 67 00 61 00 64 00 73 00> const { Buffer } = require('node:buffer'); const buf = Buffer.from('hello world', 'utf8'); console.log(buf.toString('hex')); // Prints: 68656c6c6f20776f726c64 console.log(buf.toString('base64')); // Prints: aGVsbG8gd29ybGQ= console.log(Buffer.from('fhqwhgads', 'utf8')); // Prints: <Buffer 66 68 71 77 68 67 61 64 73> console.log(Buffer.from('fhqwhgads', 'utf16le')); // Prints: <Buffer 66 00 68 00 71 00 77 00 68 00 67 00 61 00 64 00 73 00>
🌐
Attacomsian
attacomsian.com › blog › nodejs-base64-encode-decode
Encoding and decoding Base64 strings in Node.js
October 1, 2022 - To convert a string into a Base64 encoded string, we first create a buffer from the given string using the Buffer.from() method. This method takes two parameters, a plain-text string, and the character encoding, and creates a buffer or binary ...
🌐
GitHub
github.com › WebReflection › buffer-to-base64
GitHub - WebReflection/buffer-to-base64: A base64 encoder/decoder with gzip or deflate abilities. · GitHub
A base64 encoder/decoder with gzip or deflate abilities. import { encode, decode, stream } from 'buffer-to-base64'; // return a base64 string after compression const packed = await encode(buffer); // return an ArrayBuffer after decompression ...
Author: WebReflection
🌐
GitHub
gist.github.com › Deliaz › e89e9a014fea1ec47657d1aac3baa83c
Convert array buffer to base64 string · GitHub
Convert array buffer to base64 string. GitHub Gist: instantly share code, notes, and snippets.
Find elsewhere
🌐
Stack Abuse
stackabuse.com › encoding-and-decoding-base64-strings-in-node-js
Encoding and Decoding Base64 Strings in Node.js
November 25, 2024 - In the above script, we create a new buffer object and pass it our string that we want to convert to Base64. We then call the toString method on the buffer object that we just created and pass it "base64" as a parameter. The toString method with "base64" as a parameter will return data in the ...
🌐
GeeksforGeeks
geeksforgeeks.org › node.js › how-base64-encoding-and-decoding-is-done-in-node-js
How Base64 Encoding and Decoding is Done in Node.js ? - GeeksforGeeks
January 8, 2025 - Base64 encoding and decoding are when you need to encode binary data into a text format that can be easily transmitted over text-based protocols like HTTP. Node.js provides built-in methods for Base64 encoding and decoding, which can be easily performed using the Buffer class.
🌐
DEV Community
dev.to › dnature › convert-a-base64-data-into-an-image-in-node-js-3f88
Convert a Base64 data into an Image in Node.js - DEV Community
December 17, 2021 - Luckily, Node.js provides a native module called Buffer that can be used to perform Base64 encoding and decoding.
🌐
GitHub
gist.github.com › jonleighton › 958841
Encode an ArrayBuffer as a base64 string · GitHub
I finally came across this gist after a few hours of banging my head against a wall. I was fetching a PNG as an ArrayBuffer and needed to convert to a base64 encoding so I could automatically save the png. ... I was using node's Buffer to encode values and then decode in python using base64 ...
Author: Ray Rischpater
Published: 2015
Pages: 192
🌐
Medium
medium.com › @divinehycenth8 › convert-a-base64-data-into-an-image-in-node-js-d82136576e35
Convert a Base64 data into an Image in Node.js | by Divine Hycenth | Medium
November 15, 2020 - Luckily, Node.js provides a native module called Buffer that can be used to perform Base64 encoding and decoding.
🌐
Bun
bun.com › guides › utilities › base64 encoding
Encode and decode base64 data | Bun Guides
Node.js Buffer extends Uint8Array, so you can encode buffers with toBase64() and pass them to APIs that accept Uint8Array. Buffer also provides Node.js-compatible base64 decoding with Buffer.from(encoded, "base64").
🌐
GameMaker
manual.gamemaker.io › monthly › en › GameMaker_Language › GML_Reference › Buffers › buffer_base64_encode.htm
buffer_base64_encode
First the path to the image is assigned to a variable. Next the image is loaded into a buffer using buffer_load and the entire buffer's contents are encoded as a base64 string using buffer_base64_encode. The buffer is deleted after this as it is no longer needed.
🌐
Stack Overflow
stackoverflow.com › questions › 72568169 › reading-from-buffer-to-base64
Reading from buffer to base64
Now, I'm trying to read the image back from the db and decode it into a base64 string. I've tried simply this: const image = await db.get(); // here it's of type buffer return image.toString("base64"); // want to return it as type string in base64
🌐
npm
npmjs.com › package › base64-arraybuffer
base64-arraybuffer - npm
January 22, 2022 - The library encodes and decodes base64 to and from ArrayBuffers · encode(buffer) - Encodes ArrayBuffer into base64 string · decode(str) - Decodes base64 string to ArrayBuffer · You can run the test suite with: npm test · Copyright (c) 2012 ...
      » npm install base64-arraybuffer
    
Published: Jan 22, 2022
Version: 1.0.2