String Decoder

String decoder module in node.js is used to provide an API for decoding buffer object into strings. Also , this decoding is performed in a manner that UTF-8 and UTF-16 characters multibyte coding is preserved.

We can import the query string module in the following way :


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

String decoder methods

stringDecoder.write(buffer) :

write() function is used to return the specified buffer as decoded string, We pass buffer as the argument in this method.

An example is given below :


var stringDecoder = require('string_decoder').StringDecoder;
var sd = new stringDecoder('utf8');
var bufferData = Buffer('data to be buffered');
// the buffered data
console.log(bufferData); 
//  the decoded buffer  
console.log(sd.write(bufferData));

stringDecoder.end([buffer]) :

end() function is used to returns any remaining input stored in the internal buffer as a string. Bytes representing incomplete UTF-8 and UTF-16 characters will be replaced with substitution characters appropriate for the character encoding.


const StringDecoder = require('string_decoder').StringDecoder;
const decoder = new StringDecoder('utf8');

decoder.write(Buffer.from([0xE2]));
decoder.write(Buffer.from([0x82]));
console.log(decoder.end(Buffer.from([0xAC])));

About Author

Article is written by Pavan (a) KarthiQ. Well, I am serving notice period in an MNC, Bangalore. I thought to enrich every person knowledge a little, I always have a feeling, when we teach something, we will learn more than what you know. Knowledge is the only thing that doubles when you spend it.

I have also created the reporter for Protractor Jasmine. Use for your projects without any hesitation

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions