Node.js – Retrieving file character encoding

Node.jsServer Side ProgrammingProgramming

A computer system works upon bits. In the same way, a computer reads files into binary representation which is why we need to convert text characters into binary data. Most popular character encoding types types are: ASCII and Unicode. Unicode has two further types: utf8 and utf16.

Syntax

detectCharacterEncoding(filePath)

Setting Up the Environment and Execution:

Step I − Initialize the node project.

npm init

Step II − Install the required modules.

npm install detect-character-encoding

Step III − Pass the text file name whose encoding is required.

Example 1

Create a file "abc.txt" with the following text: "Welcome to Tutorials Point" and save it in the project directory.

Create a file with the name "charEncoding.js" and copy the following code snippet. After creating the file, use the command "charEncoding.js" to run this code.

// Get Character Encodning Example

// Importing the fs & characterEncoding module
const fs = require('fs');
const detectCharacterEncoding = require('detect-character-encoding');

// Passing the filename
const fileBuffer = fs.readFileSync('abc.txt');
const charsetMatch = detectCharacterEncoding(fileBuffer);
console.log(charsetMatch);

Output

C:\home\node>> node charEncoding.js
{ encoding: 'ISO-8859-1', confidence: 44 }

Example 2

Create a file "sample.txt" with the following content: "1234567890" and save it in the project directory.

// Get Character Encodning Example

// Importing the fs & characterEncoding module
const fs = require('fs');
const detectCharacterEncoding = require('detect-character-encoding');

// Passing the filename
const fileBuffer = fs.readFileSync('sample.txt');
const charsetMatch = detectCharacterEncoding(fileBuffer);

console.log(charsetMatch);

Output

C:\home\node>> node charEncoding.js
{ encoding: 'UTF-8', confidence: 15 }
raja
Published on 17-Aug-2021 11:54:21
Advertisements