The official node.js documentation for Buffer is the best place to check for something like this. As previously noted, Buffer currently supports these encodings: 'ascii', 'utf8', 'utf16le'/'ucs2', 'base64', 'base64url', 'latin1'/'binary', and 'hex'.

Answer from mscdex on Stack Overflow
🌐
W3Schools
w3schools.com › nodejs › met_buffer_tostring.asp
Node.js buffer toString() Method
The toString() method returns the buffer object according to the specified encoding.
🌐
W3schools-fa
w3schools-fa.ir › nodejs › met_buffer_tostring.html
Node.js buffer toString() Method
The toString() method returns the buffer object according to the specified encoding.
🌐
Node.js
nodejs.org › api › buffer.html
Buffer | Node.js v26.10.0 Documentation
Empty value (string, Uint8Array, Buffer) is coerced to 0. offset <integer> Number of bytes to skip before starting to fill buf. Default: 0. end <integer> Where to stop filling buf (not inclusive). Default: buf.length. encoding <string> The encoding for value if value is a string.
🌐
Educative
educative.io › answers › what-is-the-nodejs-buffertostring-method
What is the Node.js Buffer.toString() method?
The encodings supported by Node.js include utf8, utf16le, and latin1. ... start: This references the beginning data of the buffer. It starts from 0, meaning that the first data starts from 0. The default value is 0. ... The default value is ...
🌐
W3Schools
w3schools.com › nodejs › nodejs_buffer.asp
Node.js Buffer Module
// Create a string const str = 'Hello, World!'; // Convert to different encodings const utf8Buffer = Buffer.from(str, 'utf8'); console.log('UTF-8:', utf8Buffer); const base64Str = utf8Buffer.toString('base64'); console.log('Base64 string:', base64Str); const hexStr = utf8Buffer.toString('hex'); console.log('Hex string:', hexStr); // Convert back to original const fromBase64 = Buffer.from(base64Str, 'base64').toString('utf8'); console.log('From Base64:', fromBase64); const fromHex = Buffer.from(hexStr, 'hex').toString('utf8'); console.log('From Hex:', fromHex); Run example »
🌐
Medium
medium.com › @ryan_forrester_ › buffer-to-string-in-javascript-how-to-guide-b24027575419
Buffer to String in JavaScript (How To Guide) | by ryan | Medium
January 7, 2025 - The buffer is created with utf8 encoding. The buffer is then converted to a hexadecimal string using the toString method with 'hex' encoding.
🌐
W3Resource
w3resource.com › node.js › nodejs-buffer.php
Using Buffers in Node.js - w3resource<
November 13, 2023 - Converting between Buffers and JavaScript string objects requires an explicit encoding method.
🌐
Mastering JS
masteringjs.io › tutorials › node › buffer-to-string
Using the Buffer `toString()` Function in Node.js - Mastering JS
August 21, 2020 - By default, toString() converts the buffer to a string using UTF8 encoding.
Find elsewhere
🌐
W3Schools
w3schools.com › nodejs › met_buffer_from.asp
Node.js Buffer.from() Method
The Buffer.from() method creates a new buffer filled with the specified string, array, or buffer. Buffer.from(obj, encoding); ❮ Buffer Module · ★ +1 · Sign in to track progress · REMOVE ADS · Learn more about Adventure Mode› · Free ...
🌐
Readthedocs
node.readthedocs.io › en › latest › api › buffer
Buffer - node - Read the Docs
Converting between Buffers and JavaScript string objects requires an explicit encoding method.
🌐
GeeksforGeeks
geeksforgeeks.org › node-js-buffer-tostring-method
Node.js Buffer.toString() Method | GeeksforGeeks
October 13, 2021 - 'UTF-8' of complete buffer. On the next line, it returns the string with encoding style of 'UTF-8' from index 1 to 3 (here, 4 is excluded).
🌐
Adeva
adevait.com › nodejs › convert-node-js-buffer-to-string
How to Convert Node.js Buffer to String | Toptal®
May 26, 2026 - In most cases, UTF-8 is the best option for both reading and writing. Here is the full list of encodings supported by Node.js as of September/2021 (names are not case sensitive): You can also convert data in the opposite direction by creating a new Buffer from a string.
🌐
W3Schools
w3schools.com › nodejs › met_buffer_isencoding.asp
Node.js Buffer.isEncoding() Method
Check if the Buffer supports the utf8 encoding: console.log(Buffer.isEncoding('windows-1252')); Run example » · ❮ Buffer Module · ★ +1 · Sign in to track progress · REMOVE ADS · PLUS · SPACES · GET CERTIFIED · FOR TEACHERS · BOOTCAMPS · CONTACT US ·
🌐
W3Schools
w3schools.com › nodejs › met_buffer_write.asp
Node.js buffer write() Method
let buf = Buffer.from('abcdef'); buf.write('qq', 2); console.log(buf.toString()); Run example » · The write() method writes the specified string into a buffer, at the specified position. buffer.write(value, start, bytes, encoding); ❮ Buffer ...
🌐
University of New Brunswick
cs.unb.ca › ~bremner › teaching › cs2613 › books › nodejs-api › buffer
buffer
Many web pages and other document formats use UTF-8. This is the default character encoding. When decoding a Buffer into a string that does not exclusively contain valid UTF-8 data, the Unicode replacement character U+FFFD � will be used to represent those errors.
Top answer
1 of 1
3

Let's get the basics:

  • When you call Buffer.from("hello"), you map (encode) the string hello into bytes.
    (Note that hello is interpreted as a UTF8 encoded string by default, so that the above expression is equivalent to Buffer.from("hello", "utf8")).
    The resultant buffer is:
    <Buffer 68 65 6c 6c 6f>.
  • When you call Buffer.from("hello").toString(), you map (encode) hello into bytes, and then map (decode) those bytes back into a string.
    Again, note that UTF8 is the default, so the above expression is equivalent to Buffer.from("hello").toString("utf8") as well as to Buffer.from("hello", "utf8").toString("utf8").
    The resultant string is unsurprisingly:
    "hello".
    However, change it to Buffer.from("hello").toString("base64"), and you'll get:
    aGVsbG8=.

Now, let's move forward to fs.writeFileSync:

  • fs.writeFileSync(file, data[, options]) writes a file, that is: bytes. In other words, when data is a string, writeFileSync maps (encodes) that string into bytes, and then writes those bytes into the file.
    Again, the default encoding is UTF8, but when you pass some other encoding, data will be mapped (encoded) into bytes using this encoding.
    By the way, when you "open the file", be it using some text editor or even the debugger itself, you probably do it (without knowing) using UTF8 encoding.

Hopefully, You can now understand that:

  • fs.writeFileSync("myFile.xml", Buffer.from("hello","utf8").toString("base64")) maps (encodes) hello into bytes (using UTF8 encoding), and then maps (decodes) those bytes into a string (using Base64 encoding), which is aGVsbG8=.
    This is what you see when you open the file created in your last snippet.

  • fs.writeFileSync("myFile.xml", Buffer.from("hello","utf8").toString("base64"), "base64") maps (encodes) hello into bytes (using UTF8 encoding), then maps (decodes) those bytes into a string (using Base64 encoding), then maps (encodes) this string into bytes (using Base64 encoding). This is what you do in your penultimate snippet.

    To illustrate, note that this series of "mappings" is equivalent to:

    var bytes = Buffer.from(Buffer.from("hello").toString("base64"), "base64")
    

    Which gives:
    <Buffer 68 65 6c 6c 6f>.

    Then, when you open the file, you (i.e., probably your text editor) read those bytes as UTF8 by default, which is equivalent to:

    bytes.toString()  // 'bytes' is defined above
    

    Which gives:
    "hello".

🌐
Bun
bun.com › guides › binary data › buffer to string
Convert a Buffer to a string | Bun Guides
The Buffer class provides a .toString() method that converts a Buffer to a string. const buf = Buffer.from("hello"); const str = buf.toString(); // => "hello" You can optionally specify an encoding and byte range.
🌐
TutorialsPoint
tutorialspoint.com › nodejs › nodejs_buffers.htm
Node.js - Buffers
Default: 'utf8'. The encoding parameter identifies the character encoding to be used when converting string into bytes. const buf1 = Buffer.from('Hello World'); console.log(buf1); console.log(buf1.toString());