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;
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));
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])));
Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.