TextEncoder
TextDecoder
,可以进行编码之间的转换nodejs 版本需要 >=8.3.0 才支持 util.TextEncoder,util.TextDecoder
gbk 转 utf8
new TextDecoder('gbk').decode(new Uint8Array([ 47, 63, 214, 208, 206, 196, 61, 56, 56 ]).buffer)
// out: "/?中文=88"
utf8 gbk 互转
let iconv = require('iconv-lite');
// 这里是utf8
let str = '/?中文=88';
// 转换成gbk
let encoded = iconv.encode(str, 'gbk');
console.log(encoded,encoded.toJSON())
str = iconv.decode(Buffer.from([ 47, 63, 214, 208, 206, 196, 61, 56, 56 ]), 'gbk');
console.log('utf8 str:',str)
// out: Buffer <2F, 3F, D6, D0, CE, C4, 3D, 38, 38>
// Object {data: [47, 63, 214, 208, 206, 196, 61, 56, 56], type: "Buffer"}
// utf8 str: "/?中文=88"
感谢您的阅读,本文由 smallwhite’s Blog 版权所有。如若转载,请注明出处:smallwhite’s Blog(https://smallwhite.ml/pub/nodejs/nodejs-js-gbk-utf8-encode.html)