Summary of Node.js file encoding format conversion methods
This time I bring you a summary of Node.js file encoding format conversion methods, what are the precautions for Node.js file encoding format conversion, the following are practical cases, one Get up and take a look.
There are many Lua files in the project that are not in UTF-8 format. When viewed using EditPlus, they are displayed as ASCII. There are also some with BOM, which is easier to deal with. I have written about it before, and there are certain rules.
ASCII encoding is a pain in the ass. By searching online resources and repeatedly testing and comparing, I finally came up with the following more reliable method (some EditPlus display encoding is utf-8 but the node.js library returns something else. Coding>_<)
To determine whether the modification is correct, you only need to submit it through SVN after the modification, browse the submission list, double-click any file to be submitted, if the dialog box shown in the figure below is displayed, It means that the modification is successful. Others will see that the Chinese text has become garbled.
var fs = require('fs'); var chardet = require('chardet'); var jschardet = require("jschardet"); var encoding = require("encoding"); var path = "lua目录"; function readDirectory(dirPath) { if (fs.existsSync(dirPath)) { var files = fs.readdirSync(dirPath); files.forEach(function (file) { var filePath = dirPath + "/" + file; var stats = fs.statSync(filePath); if (stats.isDirectory()) { // console.log('/n读取目录:\n', filePath, "\n"); readDirectory(filePath); } else if (stats.isFile() && /\.lua$/.test(filePath)) { var buff = fs.readFileSync(filePath); if (buff.length && buff[0].toString(16).toLowerCase() == "ef" && buff[1].toString(16).toLowerCase() == "bb" && buff[2].toString(16).toLowerCase() == "bf") { //EF BB BF 239 187 191 console.log('\n发现BOM文件:', filePath, "\n"); buff = buff.slice(3); fs.writeFile(filePath, buff.toString(), "utf8"); } // { encoding: 'UTF-8', confidence: 0.99 } // var charset = chardet.detectFileSync(filePath); var info = jschardet.detect(buff); if (info.encoding == "GB2312" || info.encoding == "ascii") { var resultBuffer = encoding.convert(buff, "UTF-8", info.encoding); fs.writeFile(filePath, resultBuffer, "utf8"); } else if (info.encoding != "UTF-8" && chardet.detectFileSync(filePath) != "UTF-8") { if (buff.toString().indexOf("\r\n") > -1) { var resultBuffer = encoding.convert(buff, "UTF-8", "GBK"); fs.writeFile(filePath, resultBuffer, "utf8"); } } } }); } else { console.log('Not Found Path : ', dirPath); } } readDirectory(path);Pay attention to the above judgment. When the first one is clearly GB2312 or ascii, directly change the corresponding Convert encoding to utf-8. And if the format returned is, first determine whether there is a line break character under the PC, and if so, treat it all as GBK for processing.
The whole idea is actually relatively simple. The difficulty lies in determining the file encoding format. This is really difficult>_
Note: The files modified by the above method are consistent with the list of files that need to be submitted on Mac. At least it solves the problem I'm currently experiencing. If there is something special, you can modify the above code.
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
How to use Vue2.0 to call the camera to take pictures
How to make vue-baidu-map Enter the page to automatically locate
The above is the detailed content of Summary of Node.js file encoding format conversion methods. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Practical tips for converting full-width English letters into half-width forms. In modern life, we often come into contact with English letters, and we often need to input English letters when using computers, mobile phones and other devices. However, sometimes we encounter full-width English letters, and we need to use the half-width form. So, how to convert full-width English letters to half-width form? Here are some practical tips for you. First of all, full-width English letters and numbers refer to characters that occupy a full-width position in the input method, while half-width English letters and numbers occupy a full-width position.

AI files refer to vector graphics files created by Adobe Illustrator (AI for short) software, while CDR files refer to vector graphics files created by CorelDRAW software. Since these two softwares are developed by different manufacturers, their file formats are different and cannot be directly converted to each other. However, we can convert AI files to CDR files through some methods. A commonly used conversion method will be introduced below. Step 1: Export AI files to EPS format AdobeIllust

In this article, we will show you how to convert OpenDocumentTextDocument (ODT) files to Microsoft Word (Docx, DOC, etc.). Format. How to Convert ODT to Word in Windows 11/10 Here is how you can convert ODT documents to DOC or DOCX format on Windows PC: Convert ODT to Word using WordPad or Word The first method we are going to show you Is to use WordPad or MicrosoftWord to convert ODT to Word. Here are the steps to achieve this: First, open the WordPad app using the Start menu. Now, go to

Golang time conversion: How to convert timestamp to string In Golang, time operation is one of the very common operations. Sometimes we need to convert the timestamp into a string for easy display or storage. This article will introduce how to use Golang to convert timestamps to strings and provide specific code examples. 1. Conversion of timestamps and strings In Golang, timestamps are usually expressed in the form of integer numbers, which represent the number of seconds from January 1, 1970 to the current time. The string is

Converting a virtual machine (VM) to a physical machine is the process of migrating a virtual instance and associated application software to a physical hardware platform. This conversion helps optimize operating system performance and hardware resource utilization. This article aims to provide an in-depth look at how to make this conversion. How to implement migration from virtual machine to physical machine? Typically, the conversion process between a virtual machine and a physical machine is performed outside the virtual machine by third-party software. This process consists of multiple stages involving the configuration of virtual machines and the transfer of resources. Prepare the physical machine: The first step is to ensure that the physical machine meets the hardware requirements for Windows. We need to back up the data on a physical machine as the conversion process will overwrite the existing data. *Username and password for an administrator account with administrator rights to create system images. will be virtual

This article will introduce in detail how to convert months in PHP to English months, and give specific code examples. In PHP development, sometimes we need to convert digital months to English months, which is very practical in some date processing or data display scenarios. The implementation principles, specific code examples and precautions will be explained in detail below. 1. Implementation principle In PHP, you can convert digital months into English months by using the DateTime class and format method. Date

QQ Music allows everyone to enjoy watching movies and relieve boredom. You can use this software every day to easily satisfy your needs. A large number of high-quality songs are available for everyone to listen to. You can also download and save them. The next time you listen to them, you don’t need an Internet connection. The songs downloaded here are not in MP3 format and cannot be used on other platforms. After the membership songs expire, there is no way to listen to them again. Therefore, many friends want to convert the songs into MP3 format. Here, the editor explains You provide methods so that everyone can use them! 1. Open QQ Music on your computer, click the [Main Menu] button in the upper right corner, click [Audio Transcoding], select the [Add Song] option, and add the songs that need to be converted; 2. After adding the songs, click to select Convert to [mp3]

How to convert full-width English letters into half-width letters In daily life and work, sometimes we encounter situations where we need to convert full-width English letters into half-width letters, such as when entering computer passwords, editing documents, or designing layouts. Full-width English letters and numbers refer to characters with the same width as Chinese characters, while half-width English letters refer to characters with a narrower width. In actual operation, we need to master some simple methods to convert full-width English letters into half-width letters so that we can process text and numbers more conveniently. 1. Full-width English letters and half-width English letters
