思路:utf-8 -> decode(to buffer) -> convert to gbk(buffer also) -> write buffer to file.
var fs = require('fs');
var iconv = require('iconv-lite');
// 这里是utf8
var str = '坑爹啊,都是国际项目了,编码居然还用gbk';
console.log(new Buffer(str).length);
// 转换成gbk
var encoded = iconv.encode(str, 'gbk');
fs.writeFile('gbk.txt', encoded, function () {
var buf = fs.readFileSync('gbk.txt');
console.log(buf.length);
});
request库使用时, gb2312、GBK中文乱码解决方法
/**
* [@param](/user/param) url 需要抓取的url地址
* [@param](/user/param) calback
*/
function fetchContent(url,calback){
var req = request(url, {timeout: 10000, pool: false});
req.setMaxListeners(50);
req.setHeader('user-agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36')
.setHeader('accept', 'text/html,application/xhtml+xml');
req.on('error', function(err) {
console.log(err);
});
req.on('response', function(res) {
var bufferHelper = new BufferHelper();
res.on('data', function (chunk) {
bufferHelper.concat(chunk);
});
res.on('end',function(){
var result = iconv.decode(bufferHelper.toBuffer(),'GBK');
calback(result);
});
});
}