The readable.setEncoding() method in a Readable Stream is used to set the encoding of the data read.
Syntax:
readable.setEncoding( encoding )
Parameters: This method accepts single parameter encoding which holds the encoding type.
Return Value: It returns the data in the encoded form.
Below examples illustrate the use of readable.setEncoding() method in Node.js:
Example 1:
const fs = require( 'fs' );
const readable = fs.createReadStream( "input.txt" );
readable.setEncoding( 'hex' );
readable.on( 'data' , (chunk) => {
console.log(`${chunk}`);
});
console.log( "Program ends!!" );
|
Output:
Program ends!!
48656c6c6f
Example 2:
const fs = require( 'fs' );
const readable = fs.createReadStream( "input.txt" );
readable.setEncoding( 'base64' );
readable.on( 'data' , (chunk) => {
console.log(`${chunk}`);
});
console.log( "Program ends!!" );
|
Output:
Program ends!!
SGVs
bG8=
Reference: https://nodejs.org/api/stream.html#stream_readable_setencoding_encoding
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
12 Oct, 2021
Like Article
Save Article