新人求教:buffer转string中文乱码
 发布于 8 年前  作者 SwordsmanYao  11675 次预览  最后一次回复是 8 年前  来自 问答 

buffer 转 string 显示的时候乱码,操作系统是windows,代码如下:

var buf = new Buffer("test测试","utf8");

console.log(buf);

console.log(buf.length);

console.log(buf.toString("utf8"));

输出: E:\test\nodejstest>node test.js <Buffer 74 65 73 74 ef bf bd ef bf bd ef bf bd ef bf bd> 16 test����

5 回复
Qiubaowei

我的正常 QQ截图20170414155703.png 而且长度好像是10

SwordsmanYao

@Qiubaowei (⊙o⊙)…我装的是假node吗

SwordsmanYao

@Qiubaowei 我知道了 我的js文件是gbk的 改成 utf8 就好了 谢了

jpuncle

文件是什么格式的?

SwordsmanYao

@jpuncle 是 js 文件编码问题,之前是 gbk 的