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:
Answer from onteria_ on Stack Overflow'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.
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.
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.
buffer.write(buffer, 'base64')
Convert String to base64
node.js - NodeJS: How to decode base64 encoded string back to binary? - Stack Overflow
using Buffer.from(binaryData).toString('base64') produces invalid base64 data
Hi,
I'm working with an API that requires me to convert the client id and the client secret to base64 encoded.
I've found the function btoa() but it is deprecated. Then I tried
const encodedString = Buffer.from(myString, 'base64');
That didn't work either. I couldn't find anything on stack overflow. Anyone know how to convert strings to base64? Thanks in advance!
For context, I'm trying to store images in blob format in an sqlite database. I send the image in post data in base64 format, then when the request is received in express, I run
Buffer.from(req.body.img, 'base64'); to convert it into binary and store it in the database. However, when I retrieve the data and convert it back to base64, it produces an invalid base64 string like
dataimage/jpgbase64<base64data> when it should be data:image/jpg;base64,<base64data>
I'm using prisma to manage my database.
Is this an issue with the actual encoding/decoding, prisma, or sqlite?
Edit: encoding the base64 into binary, and then immediately encoding the binary back into base64 (without saving to the database) produces the same issue.