node js - Node js StringDecoder - node - node js tutorial - webnode




What is String Decoder in node.js?

  • The Node.js StringDecoder is used to decode buffer into string.
  • It is similar to buffer.toString() but provides extra support to UTF.
  • You need to use require('string_decoder') to use StringDecoder module.

Syntax:

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

Node.js StringDecoder Methods

  • StringDecoder class has two methods only.
Method Description
decoder.write(buffer) It is used to return the decoded string.
decoder.end() It is used to return trailing bytes, if any left in the buffer.
Related nodejs article tags - node js - node js tutorial - node js examples

Node.js StringDecoder Example

  • Let's see a simple example of Node.js StringDecoder.
const StringDecoder = require('string_decoder').StringDecoder;  
const decoder = new StringDecoder('utf8');  
  
const buf1 = new Buffer('this is a test');  
console.log(decoder.write(buf1));//prints: this is a test  
  
const buf2 = new Buffer('7468697320697320612074c3a97374', 'hex');  
console.log(decoder.write(buf2));//prints: this is a test  
  
const buf3 = Buffer.from([0x62,0x75,0x66,0x66,0x65,0x72]);  
console.log(decoder.write(buf3));//prints: buffer  

Output:

 Nodejs Stringdecoder Example

Learn Node js - node js Tutorial - Nodejs Stringdecoder Example - node - Node js Examples


This wikitechy technological portal provides you whole lot of information related to the topics such as mongodb tutorial , what is node js , express js tutorial , node js tutorial pdf , learn node js , node js tutorial for beginners , node js tutorial w3schools , node js express , javascript for beginners , node js express tutorial , node js tutorial point , node js book , node js server , note js , node js mongodb , what is node js used for , why node js , node tutorial , node js basics , tutorialspoint node js , node express , node js sample application , nodeschool , node js for beginners , express tutorial , node js application , use of node js , npm tutorial , node js org , node js training , node js online training , node js tutorial beginner , how to use node js , node js hello world , express node js , node js mongodb tutorial , nodejs http , why use node js

Related Searches to Node.js StringDecoder