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
🌐
Node.js
nodejs.org › api › buffer.html
Buffer | Node.js v26.10.0 Documentation
Node.js also supports the following binary-to-text encodings. For binary-to-text encodings, the naming convention is reversed: Converting a Buffer into a string is typically referred to as encoding, and converting a string into a Buffer as decoding.
🌐
Educative
educative.io › answers › what-is-the-nodejs-buffertostring-method
What is the Node.js Buffer.toString() method?
In Node.js, the Buffer.toString() method is used to decode or convert a buffer to a string, according to the specified character encoding type.
🌐
Toptal
toptal.com › external-blogs › adeva › convert-node-js-buffer-to-string
How to Convert Node.js Buffer to String | Toptal®
May 26, 2026 - You’ll usually want the default UTF-8 encoding, but it’s possible to indicate a different encoding if needed. To convert from a string to a Buffer object, use the static Buffer.from() method - again optionally passing the encoding.
🌐
Mastering JS
masteringjs.io › tutorials › node › buffer-to-string
Using the Buffer `toString()` Function in Node.js - Mastering JS
August 21, 2020 - const buf = Buffer.from('Hello, World', 'utf8'); buf.toString(); // 'Hello, World' The toString() method's first parameter is a string called encoding. It determines what format Node.js uses to express the raw data.
🌐
W3Schools
w3schools.com › nodejs › met_buffer_tostring.asp
Node.js buffer toString() Method
Node.js Compiler Node.js Server ... console.log(buf.toString()); Run example » · The toString() method returns the buffer object according to the specified encoding....
🌐
GeeksforGeeks
geeksforgeeks.org › node-js-buffer-tostring-method
Node.js Buffer.toString() Method | GeeksforGeeks
October 13, 2021 - Example 1: ... // Node.js program to demonstrate the // Buffer.toString() Method // Creating a buffer var buffer = new Buffer.alloc(5); // Loop to add value to the buffer for (var i = 0; i < 5; i++) { buffer[i] = i + 97; } // Display the value ...
🌐
Melvin George
melvingeorge.me › blog › convert-string-buffer-nodejs
How to convert a String to Buffer and vice versa in Node.js | MELVIN GEORGE
September 19, 2020 - // buffer const str = "Hey. this is a string!"; const buff = Buffer.from(str, "utf-8"); // convert buffer to string const resultStr = buff.toString(); console.log(resultStr); //Hey. this is a string!
🌐
DEV Community
dev.to › melvin2016 › how-to-convert-a-string-to-buffer-and-vice-versa-in-node-js-5op
How to convert a String to Buffer and vice versa in Node.js - DEV Community
September 19, 2020 - // buffer const str = "Hey. this is a string!"; const buff = Buffer.from(str, "utf-8"); // convert buffer to string const resultStr = buff.toString(); console.log(resultStr); //Hey. this is a string!
Find elsewhere
🌐
Readthedocs
node.readthedocs.io › en › latest › api › buffer
Buffer - node - Read the Docs
Decodes and returns a string from buffer data encoded using the specified character set encoding.
🌐
Medium
medium.com › hackernoon › https-medium-com-amanhimself-converting-a-buffer-to-json-and-utf8-strings-in-nodejs-2150b1e3de57
Converting a Buffer to JSON and Utf8 Strings in Nodejs | by Aman Mittal | HackerNoon.com | Medium
April 1, 2019 - The other way to convert a buffer to a string is using StringDecoder core module from Nodejs API. Want to receive more articles like this one? Subscribe me here. Sometimes, I send “never seen before” content to my subscribers.
🌐
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 converted to a string with utf8 encoding and displayed. Node.js provides robust support for buffers and their conversion to strings.
🌐
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.
🌐
W3Resource
w3resource.com › node.js › nodejs-buffer.php
Using Buffers in Node.js - w3resource<
November 13, 2023 - The Buffer class is global. It deals with binary data directly and can be constructed in a variety of ways. Converting between Buffers and JavaScript string objects requires an explicit encoding method.
Top answer
1 of 2
319

Single Buffer

If you have a single Buffer you can use its toString method that will convert all or part of the binary contents to a string using a specific encoding. It defaults to utf8 if you don't provide a parameter, but I've explicitly set the encoding in this example.

var req = http.request(reqOptions, function(res) {
    ...

    res.on('data', function(chunk) {
        var textChunk = chunk.toString('utf8');
        // process utf8 text chunk
    });
});

Streamed Buffers

If you have streamed buffers like in the question above where the first byte of a multi-byte UTF8-character may be contained in the first Buffer (chunk) and the second byte in the second Buffer then you should use a StringDecoder. :

var StringDecoder = require('string_decoder').StringDecoder;

var req = http.request(reqOptions, function(res) {
    ...
    var decoder = new StringDecoder('utf8');

    res.on('data', function(chunk) {
        var textChunk = decoder.write(chunk);
        // process utf8 text chunk
    });
});

This way bytes of incomplete characters are buffered by the StringDecoder until all required bytes were written to the decoder.

2 of 2
-5
var fs = require("fs");

function readFileLineByLine(filename, processline) {
    var stream = fs.createReadStream(filename);
    var s = "";
    stream.on("data", function(data) {
        s += data.toString('utf8');
        var lines = s.split("\n");
        for (var i = 0; i < lines.length - 1; i++)
            processline(lines[i]);
        s = lines[lines.length - 1];
    });

    stream.on("end",function() {
        var lines = s.split("\n");
        for (var i = 0; i < lines.length; i++)
            processline(lines[i]);
    });
}

var linenumber = 0;
readFileLineByLine(filename, function(line) {
    console.log(++linenumber + " -- " + line);
});
🌐
Node.js
nodejs.org › api › string_decoder.html
String decoder | Node.js v26.7.0 Documentation
The node:string_decoder module provides an API for decoding Buffer objects into strings in a manner that preserves encoded multi-byte UTF-8 and UTF-16 characters.
🌐
Node.js
nodejs.org › download › release › v10.2.0 › docs › api › buffer.html
Buffer | Node.js v10.2.0 Documentation
The character encodings currently supported by Node.js include: 'ascii' - For 7-bit ASCII data only. This encoding is fast and will strip the high bit if set. 'utf8' - Multibyte encoded Unicode characters. Many web pages and other document formats use UTF-8. 'utf16le' - 2 or 4 bytes, little-endian ...
🌐
GitHub
github.com › node-fetch › node-fetch › issues › 1218
[Japanese encoding] convert buffer to string in correct encoding · Issue #1218 · node-fetch/node-fetch
July 22, 2021 - The first step will be detect original encoding of buffer · The second step will be convert original to utf-8 encoding · Or to be friendly we should provide some useful parameter to async text() like autodetectEncoding = true, destinationEncoding with default value is UTF-8 ... const fetch = require('node-fetch'); fetch( 'https://www.leopalace21.com/apps/tradeCondition/logonAction.do;jsessionid=0001W7h94kP1CI4B8v272KUswNN:17abad7vp?fbclid=IwAR3rHpV6wO6-cqW7d7YM7W-J7mcOyDxTYP57EjgLy19K-yV737k0dT4yUdo', { referrerPolicy: 'strict-origin-when-cross-origin', body: null, method: 'GET', mode: 'cors', }, ) .then((res) => res.buffer()) .then((buffer) => { const decoder = new TextDecoder('shift_jis'); const r = decoder.decode(buffer); console.log(r); });
Author: node-fetch
🌐
npm
npmjs.com › package › string-encode
string-encode - npm
September 23, 2020 - ... The most important functions of this library are str2buffer(str, asUtf8) and buffer2str(buf, asUtf8) for converting any String, including multibyte, to and from Uint8Array. import { str2buffer, buffer2str } from 'string-encode';
      » npm install string-encode
    
Published: Sep 23, 2020
Version: 0.2.2