Home > Web Front-end > Front-end Q&A > How to convert nodejs utf8 to gb2312

How to convert nodejs utf8 to gb2312

PHPz
Release: 2023-04-05 10:32:14
Original
1152 people have browsed it

Node.js is an open source, cross-platform JavaScript running environment built on the Chrome JavaScript engine. It allows JavaScript to run on the server side. It is a powerful tool for front-end development and one of the choices for back-end development. In the actual development process, we sometimes need to convert character set encoding, such as UTF-8 to GB2312. So how to perform the corresponding operation in Node.js? Let’s introduce it below.

First, let’s understand what encoding and character sets are. Encoding is a process of converting characters into binary, while a character set is an unordered collection of characters whose correspondence represents the same encoding rule. Usually, the common character set encoding formats include ANSI, UTF-8, GB2312 and other formats.

Node.js provides some built-in modules for character set conversion operations, including iconv-lite, iconv, etc. Here, we use iconv-lite for example demonstration.

First, in Node.js, we need to install the corresponding iconv-lite dependencies, which can be installed directly using npm:

$ npm install iconv-lite --save
Copy after login

Next, enter the formal use process. First, we convert the string in UTF-8 format into a Buffer in GB2312 format:

const iconv = require('iconv-lite');
const str = "这是一段UTF-8编码格式的字符串";

// 将UTF-8字符串转换成GB2312编码格式的Buffer
let strBuffer = iconv.encode(str, 'gb2312');
console.log(strBuffer);
Copy after login

After executing the above code, you can see the output Buffer in GB2312 encoding format on the console.

Next, we convert the Buffer in GB2312 encoding format into a string in UTF-8 format:

const iconv = require('iconv-lite');
const gbStr = '这是一段GB2312编码格式的字符串';

// 将GB2312编码格式的Buffer转换成UTF-8格式的字符串
let gbBuffer = Buffer.from(gbStr, 'binary');
let utf8Str = iconv.decode(gbBuffer, 'gb2312');
console.log(utf8Str);
Copy after login

After executing the above code, you can see the output UTF-8 encoding on the console Format string.

Of course, in actual development, we may need to convert the encoding format of the entire file. At this time, you can use the transformStream() method of iconv-lite to achieve:

const fs = require('fs');
const iconv = require('iconv-lite');

// 将UTF-8编码格式的文件转换成GB2312编码格式的文件
let readStream = fs.createReadStream('utf-8.txt');
let writeStream = fs.createWriteStream('gb2312.txt');
let transformStream = iconv.decodeStream('utf-8');
transformStream.pipe(iconv.encodeStream('gb2312')).pipe(writeStream);
readStream.pipe(transformStream);
Copy after login

In the above code, we create a readable stream through the createReadStream() method, then create a writable stream, and use iconv.decodeStream () method to convert it into UTF-8 encoding format, and then transfer the data stream through the pipe() method. Finally, after executing the above code, a file in GB2312 encoding format can be generated in the current directory.

In short, in Node.js, we can use built-in modules such as iconv-lite to convert character set encoding formats to improve development efficiency and code quality.

The above is the detailed content of How to convert nodejs utf8 to gb2312. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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