- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Node.js – Retrieving file character encoding
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
ode>> 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
ode>> node charEncoding.js { encoding: 'UTF-8', confidence: 15 }