Must install a string_decode package in your project for using string decode to create buffer object to pass in parameters value in the constructor.

npm install  string_decoder --save


The String Decoder module provides API for decoding the Buffer objects into strings. It is similar to the buffer.toString() but provides extra support to multi-byte UTF-8 and UTF-16 characters. Which can be accessed using

Decode Write

Returns a decoded string, secure that any incomplete multibyte characters at the end of the Buffer are dropped from the returned string and stored in an internal buffer for the next call to stringDecoder.write() or stringDecoder.end()


A Buffer consists of the bytes to decode


This syntax using for String Decoder module in your application

const { StringDecoder } = require('string_decoder');


String Decoder class has two methods



 end() Returns what remains of the input stored in the internal buffer


 write() Returns the specified buffer as a string



//UTF-8 is a variable-length  character encoding for Unicode. The '8' means it uses 8-bit blocks to  represent a character
// HTML text for   readable after it will converting into UTF8 Unicode format.
//Write Buffer Value.
//After converted buffer value content

var string_decode = require('string_decoder').StringDecoder;
var obj = new string_decode('utf8');       
var obj_different = Buffer('Plain HTML text for reading  after it will  convert into UTF-8 Unicode');                        


<Buffer 50 6 c 61 69 6e 20 48 54 4 d 4 c 20 74 65 78 65 6 f 72 20 72 65 61 64 69 67 20 61 66 74 65 72 20 69 74 20 77 69 6 c 6 c 20 63 6 f 6e 76 65 72 74.. >
Plain HTML text for reading after it will convert into UTF8 Unicode

When a Buffer instance is written to the String Decoder instance, an internal buffer is used to ensure that the decoded string does not contain any incomplete multibyte characters. They will remain in Buffer until the next call to the stringDecoder.write() or until stringDecoder.end() is called.

In the code, the three UTF-8 encoded bytes of the European Euro symbol (€) are written over three separate operations.StringDecoder is better in UTF-8, Let’s see some practical example, Here we have declared

var b1 = new Buffer([0xe0,0xb8,0x81,0xe0,0xb8,0xb2,0xe0,0xb8])
, b2 = new Buffer([0xa3,0xe0,0xb8,0x97,0xe0,0xb8,0x94,0xe0])
, b3 = new Buffer([0xb8,0xaa,0xe0,0xb8,0xad,0xe0,0xb8,0x9a])

We received from these buffers one at a time.
When we receive each of these buffers and pass it immediately to the client in a string format.Then for each received buffer, decoded it and sent it to right away.


NOTE -: toString() to pass the Buffer value to the client as a “ String ” format.

Decode End

const StringDecoder = require('string_decoder').StringDecoder;
const decoder = new StringDecoder('utf8');
const buf1 = new Buffer('this is a test');
console.log(decoder.end(buf1));//This is printed simple text format "this is a test"

const buf2 = new Buffer('7468697320697320612074c3a97374', 'hex');
console.log(decoder.end(buf2));//prints: "this is a test"
const buf3 = Buffer.from([0x62,0x75,0x66,0x66,0x65,0x72]);
console.log(decoder.end(buf3));//prints: buffer value

It returns rest of input stored in the remaining buffer as a string. Bytes interpreting incomplete UTF-8 and UTF-16 characters will be replaced with substitution characters appropriate for the character encoding.

If the buffer argument is provided, one last call to stringDecoder.write() is performed before returning the remaining input.


This is test content
This is test content

Buffer from

The Buffer.from() method creates an advanced buffer filled with the specified string, array, or buffer.

Learn More-