Node.js Tutorial
- Node.js Tutorial
- Install Node.js on Windows
- Install Node.js on Linux/Ubuntu/CentOS
- Node.js First Example
- Node.js Console
- Node.js REPL
- Node.js Package Manager
- Node.js Command Line Options
- Node.js Global Objects
- Node.js OS
- Node.js Timer
- Node.js Errors
- Node.js DNS
- Node.js Net
- Node.js Crypto
- Node.js TLS/SSL
- Node.js Debugger
- Node.js Process
- Node.js Child Process
- Node.js Buffers
- Node.js Streams
- Node.js File System (FS)
- Node.js Path
- Node.js StringDecoder
- Node.js Query String
- Node.js ZLIB
- Node.js Assertion Testing
- Node.js V8
- Node.js Callbacks
- Node.js Events
- Node.js Punycode
- Node.js TTY
- Node.js Web Module
- NestJS
Node.js MySQL
Node.js MongoDB
Nodejs Difference
Node.js MCQ
Node.js Express
Nodejs Interview Questions
Node.js StringDecoder
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.
- 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. |
Node.js StringDecoder Example
Let's see a simple example of Node.js StringDecoder.
File: stringdecoder_example1.js
- 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