Created
February 17, 2014 07:58
-
-
Save JacksonTian/9046532 to your computer and use it in GitHub Desktop.
utf8 => gbk
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
The
encoding
option is ignored ifdata
is a buffer. It defaults to'utf8'
.
被 sublime 坑了,原来早就转成功了,晕死
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
我就是这么写的,但没用
fs.writeFile
的data
参数如果是 buffer,就不能指定 encoding,只能是utf8
如果把写入的 data 转成 string,是可以指定 encoding,但不支持
gbk
所以,无果