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
The character encodings currently ... a Buffer into a string that does not exclusively contain valid UTF-8 data, the Unicode replacement character U+FFFD � will be used to ......
🌐
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 - To convert from a string to a Buffer object, use the static Buffer.from() method - again optionally passing the encoding. If you’re a Node.js developer interested in advancing ...
🌐
Mastering JS
masteringjs.io › tutorials › node › buffer-to-string
Using the Buffer `toString()` Function in Node.js - Mastering JS
August 21, 2020 - The most commonly used are: 'utf8' 'hex' 'base64' For example, by calling .toString('hex'), you get a string representation of the buffer where each byte is encoded as 2 hexadecimal characters. const fs = require('fs'); const buf = ...
🌐
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 - abcde bcd 6162636465 Explanation: In the above example, we have declared a variable buffer with size of 5 and have filled with ASCII value from 'a' to 'e'. Next, we have used toString() method without any parameters, that returns the string ...
🌐
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!
🌐
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.
Find elsewhere
🌐
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!
🌐
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 created with utf8 encoding. The buffer is then converted to a hexadecimal string using the toString method with 'hex' encoding.
🌐
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.
🌐
TutorialsPoint
tutorialspoint.com › nodejs › nodejs_buffer_tostring_method.htm
Node.js - Buffer.toString() Method
The NodeJS Buffer.toString() method helps to decode the string as per the encoding given. By default, the encoding used is 'utf-8'. Following is the syntax of the Node.JS Buffer.toString() Method − This method accepts three parameters but all are
🌐
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.
🌐
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 - /** * Decode response as text * * @return Promise */ async text() { const buffer = await consumeBody(this); return buffer.toString(); // <= toString function will convert buffer to string with encoding is UTF-8. But the correct encoding in my ...
Author: node-fetch
🌐
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.
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 › docs › v0.5.0 › api › buffers.html
buffers - Node.js Manual & Documentation
Raw data is stored in instances ... be resized. The Buffer object is global. Converting between Buffers and JavaScript string objects requires an explicit encoding method....