Skip to content

Instantly share code, notes, and snippets.

@JacksonTian
Created February 17, 2014 07:58
Show Gist options
  • Save JacksonTian/9046532 to your computer and use it in GitHub Desktop.
Save JacksonTian/9046532 to your computer and use it in GitHub Desktop.
utf8 => gbk
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);
});
@antife-yinyue
Copy link

我就是这么写的,但没用

fs.writeFiledata 参数如果是 buffer,就不能指定 encoding,只能是 utf8

如果把写入的 data 转成 string,是可以指定 encoding,但不支持 gbk

所以,无果

@antife-yinyue
Copy link

The encoding option is ignored if data is a buffer. It defaults to 'utf8'.

@antife-yinyue
Copy link

被 sublime 坑了,原来早就转成功了,晕死

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment