简体   繁体   English

非ASCII字符作为参数

[英]non-ascii char as arguments

printargv.js: printargv.js:

console.log(Buffer.byteLength(process.argv[2]));

In cmd.exe (with chcp=65001,font='Lucida Console'), I ran: 在cmd.exe中(使用chcp = 65001,font ='Lucida Console'),我运行了:

node printargv.js Ā 

(Note: unicode code point of Ā is U+0100.) The script outputted: (注意:Ā的Unicode代码点是U + 0100。)输出的脚本:

1 1个

I expected the script to print a number greater than 1 but it doesn't. 我希望脚本打印出大于1的数字,但事实并非如此。 Does anyone know why? 有人知道为什么吗?

edit: i think that node 'parses' initial arguments incorrectly for cmd.exe after i tried the below code: 编辑:我尝试以下代码后,认为该节点为cmd.exe错误地“解析”了初始参数:

var i = require('readline').createInterface(process.stdin,process.stdout);

i.question('char: ', function(c){
  console.log( Buffer.byteLength(c) );
  i.close();
  process.stdin.destroy();
});

the output is 2 输出是2

Your program is not receiving the Ā , it's receiving an A instead. 您的程序没有收到Ā ,而是收到了A I used this program to test: 我使用此程序进行测试:

var n;
for (n = 0; n < process.argv.length; ++n) {
    console.log(n + ": '" + process.argv[n] + "'");
}
console.log("length: " + process.argv[2].length);
console.log("code: " + process.argv[2].charCodeAt(0));
console.log("size: " + Buffer.byteLength(process.argv[2]));

On Ubuntu using UTF-8 in the console, I got: 在Ubuntu中,在控制台中使用UTF-8,我得到了:

$ node test.js Ā
0: 'node'
1: '/home/tjc/temp/test.js'
2: 'Ā'
length: 1
code: 256
size: 2

...which is correct. ...哪个是对的。

On Windows 7 using chcp 65001 and Lucida Console, I got: 在使用chcp 65001和Lucida Console的Windows 7上,我得到了:

C:\tmp>node temp.js Ā
0: 'node'
1: 'C:\tmp\temp.js'
2: 'A'
length: 1
code: 65
size: 1

Note that the Ā became an A at some point along the way. 请注意, Ā在此过程中的某个时刻变成了A

As I said in my comment on the question, I can only assume there's some issue with Lucida Console , or cmd.exe's handling of UTF-8, or perhaps node.exe 's handling of Unicode from the console on Windows (I used the pre-built 0.5.7 version ). 正如我在对该问题的评论中所说的那样,我只能假设Lucida Console或cmd.exe对UTF-8的处理,或者node.exe对Windows中控制台的Unicode的处理存在一些问题(我使用了预先建立的0.5.7版本 )。


Update : This might be something to take up with the NodeJS folks, since Windows appears to get it right on its own. 更新 :这可能是NodeJS团队需要解决的问题,因为Windows似乎可以独立完成它。 If I put this code in a test.vbs file: 如果我将此代码放在test.vbs文件中:

WScript.Echo WScript.Arguments(0)
WScript.Echo AscW(WScript.Arguments(0))

I get a correct result: 我得到正确的结果:

C:\tmp>cscript /nologo test.vbs Ā
Ā
256

...suggesting that the terminal is passing the argument correctly to the program. ...建议终端将参数正确传递给程序。 So it could be an issue with the Windows node.exe build. 因此,这可能是Windows node.exe构建的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM