Home > Web Front-end > JS Tutorial > node uses iconv-lite to transcode 'gbk' format

node uses iconv-lite to transcode 'gbk' format

青灯夜游
Release: 2020-11-19 17:44:50
forward
3745 people have browsed it

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'));
Copy after login

.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);
Copy after login

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!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template