当我想要的只是 ASCII 时,我认为 JS 正在给我 Unicode
I think JS is giving me Unicode when all I want is ASCII
我正在尝试获取一个字符并对其进行递增或递减。我正在尝试使用 String.fromCharCode
来完成此操作。虽然以下内容在控制台中有效
'Na' + String.fromCharCode(78 + n) // n is 2 in this example, can even hard-code it
似乎工作正常并给我 NaP
,我的代码中的其他东西却给了我 Na̎
。
这是正在执行的代码块
if (ifTypes(a, b, 'integer', 'NaN')) { // disregard this, inside code IS executing
console.log("a: " + JSON.stringify(a) + " b: " + JSON.stringify(b));
var n = a[1] === 'NaN' ? b[0] : a[0];
var output = 'Na' + String.fromCharCode(78 + n);
console.log("output: " + output);
return output;
}
从控制台验证:
a: [null,"NaN"] b: ["2","integer"]
output: Na̎ // <-- SO's code highlighter is messing that up
是的,如果有人认出我在做什么,那我就是 implementing the interpreter from xkcd's 1537. If you want to see this code in action and maybe try to read through my console output, I've got it online here。在打火机栏中点击回车即可,模拟终端而已
我怀疑这个问题有些奇怪 ascii/unicode toggle。我试过将 'Na'
放在 String.fromCharCode
中,但结果相似。虽然 a
应该是 [NaN, "NaN"]
,但我认为这不是问题所在。我也确实需要追踪那个错误。
我认为问题在于您要添加 "2"
和 78
,而不是 2
和 78
。
function print(s) {
document.querySelector("#result").innerHTML += "<pre>" + s + "</pre>";
}
var a = [null, "NaN"];
var b = ["2", "integer"];
var n = a[1] === 'NaN' ? b[0] : a[0];
var output = 'Na' + String.fromCharCode(78 + n);
print("output 1: " + output);
output = 'Na' + String.fromCharCode(78 + (typeof n == "number" ? n : parseInt(n, 10)));
print("output 2: " + output);
<div id="result"></div>
n
是一个字符串,而不是一个整数,所以加法没有达到您的预期。
改变
var output = 'Na' + String.fromCharCode(78 + n);
到
var output = 'Na' + String.fromCharCode(78 + parseInt(n));
我正在尝试获取一个字符并对其进行递增或递减。我正在尝试使用 String.fromCharCode
来完成此操作。虽然以下内容在控制台中有效
'Na' + String.fromCharCode(78 + n) // n is 2 in this example, can even hard-code it
似乎工作正常并给我 NaP
,我的代码中的其他东西却给了我 Na̎
。
这是正在执行的代码块
if (ifTypes(a, b, 'integer', 'NaN')) { // disregard this, inside code IS executing
console.log("a: " + JSON.stringify(a) + " b: " + JSON.stringify(b));
var n = a[1] === 'NaN' ? b[0] : a[0];
var output = 'Na' + String.fromCharCode(78 + n);
console.log("output: " + output);
return output;
}
从控制台验证:
a: [null,"NaN"] b: ["2","integer"]
output: Na̎ // <-- SO's code highlighter is messing that up
是的,如果有人认出我在做什么,那我就是 implementing the interpreter from xkcd's 1537. If you want to see this code in action and maybe try to read through my console output, I've got it online here。在打火机栏中点击回车即可,模拟终端而已
我怀疑这个问题有些奇怪 ascii/unicode toggle。我试过将 'Na'
放在 String.fromCharCode
中,但结果相似。虽然 a
应该是 [NaN, "NaN"]
,但我认为这不是问题所在。我也确实需要追踪那个错误。
我认为问题在于您要添加 "2"
和 78
,而不是 2
和 78
。
function print(s) {
document.querySelector("#result").innerHTML += "<pre>" + s + "</pre>";
}
var a = [null, "NaN"];
var b = ["2", "integer"];
var n = a[1] === 'NaN' ? b[0] : a[0];
var output = 'Na' + String.fromCharCode(78 + n);
print("output 1: " + output);
output = 'Na' + String.fromCharCode(78 + (typeof n == "number" ? n : parseInt(n, 10)));
print("output 2: " + output);
<div id="result"></div>
n
是一个字符串,而不是一个整数,所以加法没有达到您的预期。
改变
var output = 'Na' + String.fromCharCode(78 + n);
到
var output = 'Na' + String.fromCharCode(78 + parseInt(n));