console.log 输出中文乱码,怎么解决?
发布于 12 年前 作者 ajie0112 26597 次浏览 最后一次编辑是 8 年前

刚开始研究node.js, 遇到了几个问题,请各位前辈达人指点下: 1.console.log 输出中文乱码 2.中文字符串的长度获取怪异 代码如下:

var print = console.log;
var str = "中文";
print(str.length, str);

var str2 = new String("中文");
print(str2.length, str2);

print(Buffer.byteLength("中文", "utf8"));
print(Buffer.byteLength("中文", "ucs2"));

var str3 = new Buffer("中文", "utf8");
print(str3.length, str3);

var str4 = new Buffer("中文", "ucs2");
print(str4.length, str4);

输出如下:

4 '����'
4 { '0': '�', '1': '�', '2': '�', '3': '�' }
12
8
12 <Buffer ef bf bd ef bf bd ef bf bd ef bf bd>
8 <Buffer fd ff fd ff fd ff fd ff>
请按任意键继续. . .

请问各位如何解决上述问题?

4 回复

嗯,是在windows平台开发的。按你说的方法OK了,非常谢谢!

谢谢…我的也是这个问题!

今后开发中涉及编码的问题太多了,最好的方式就是用专门的工具设置所有的js文件都是utf8

用 UltraEdit 等编辑工具,打开下面状态栏就回显示字符集,也可以做转换

回到顶部