Node.js stringDecoder.write() Method

Last Updated : 3 Jun, 2026

The stringDecoder.write() method in Node.js is used to decode data from a Buffer, TypedArray, or DataView into a string. It helps correctly process character data by handling incomplete multibyte characters across multiple chunks.

  • Stores incomplete multibyte characters in an internal buffer.
  • Uses the stored bytes in subsequent write() or end() calls.
  • Commonly used when reading data from streams in chunks.

Syntax:

stringDecoder.write( buffer )

where,

  • buffer: It is a Buffer, TypedArray or DataView that contains the bytes that have to be decoded.

Return Value: It returns a string which is decoded from the given buffer.

Example 1:

javascript
// Node.js program to demonstrate the   
// stringDecoder.write() Method 

// Import the string_decoder module
// and get the StringDecoder class 
// using object destructuring
const { StringDecoder } = require("string_decoder");

// Create a new instance of the decoder
const decoder = new StringDecoder("utf-8");

const text_one = Buffer.from("GeeksforGeeks", "utf-8");
let decoded_text = decoder.write(text_one);
console.log("Decoded Text:", decoded_text);

In this code, A StringDecoder object is created with UTF-8 encoding to decode data stored in a Buffer. The write() method converts the buffer content into a readable string and returns the decoded result.

Output:

Screenshot-2026-06-02-163757

Example 2:

javascript
// Node.js program to demonstrate the   
// stringDecoder.write() Method 

// Import the string_decoder module
// and get the StringDecoder class 
// using object destructuring
const { StringDecoder } = require("string_decoder");

// Create a new instance of the decoder
const decoder = new StringDecoder("utf-8");

// Decoding text using hex from buffer
const hex_text = new Buffer.from(
      "4765656B73666F724765656B73", "hex");
let decoded_hex_text = decoder.write(hex_text);
console.log("Decoded Text Hex:", decoded_hex_text);

// Decoding text using base64 from buffer
const base64_text = new Buffer.from(
      "R2Vla3Nmb3JHZWVrcw==", "base64");
let decoded_base64_text = decoder.write(base64_text);
console.log("Decoded Text Base64:", decoded_base64_text);

// Decoding the cent symbol from buffer
const cent_symbol = Buffer.from([0xc2, 0xa2]);
let cent_symbol_out = decoder.write(cent_symbol);
console.log("Cent Symbol:", cent_symbol_out);

In this code, The StringDecoder object is used to decode data stored in buffers with different encodings. It converts hexadecimal, Base64, and byte-based buffer data into readable UTF-8 text and symbols.

Output:

Screenshot-2026-06-02-163645

Use cases of stringDecoder.write()

  • Reading text files chunk by chunk without corrupting characters.
  • Processing incoming data from sockets and network connections.
  • Decoding hexadecimal and Base64 encoded buffer data.
  • Displaying human-readable text from binary data sources.
  • Handling Unicode characters that may be split across data chunks.

Reference: https://nodejs.org/api/string_decoder.html#string_decoder_stringdecoder_write_buffer

Comment

Explore