Home>Article>Web Front-end> node uses iconv-lite to transcode "gbk" format
Related recommendations: "node js tutorial"
In window, gbk and utf-8 are the two most common ones format, but we often need to convert GBK to UTF-8 when displaying. I now have an operation to read files synchronously: The content in
const fs = require('fs'); const path = require('path'); const buffer = fs.readFileSync(path.join(__dirname, '../lyrics/友谊之光.lrc'));
.lrc is garbled in gbk format, then what should I do? How to do it? Some people may think of adding a "utf8" attribute to readFileSync, but the result is unsatisfactory. Here we need to introduce a node plug-in called iconv-lite. The complete code is as follows:
const fs = require('fs'); const path = require('path'); // 将文本读取到一个buffer中 const buffer = fs.readFileSync(path.join(__dirname, '../lyrics/友谊之光.lrc')); // 由于Windows下文件默认编码为GBK所以需要通过 const iconv = require('iconv-lite'); const content2 = iconv.decode(buffer,'gbk'); console.log(content2);
For more programming-related knowledge, please visit:Programming Video Course! !
The above is the detailed content of node uses iconv-lite to transcode "gbk" format. For more information, please follow other related articles on the PHP Chinese website!