我正在尝试将以下内容从 Java 移植到 JavaScript:
String key1 = "whatever";
String otherKey = "blah";
String key2;
byte keyBytes[] = key1.getBytes();
for (int i = 0; i < keyBytes.length; i++) {
keyBytes[i] ^= otherKey.charAt(i % otherKey.length());
}
key2 = new String(keyBytes);
这是我写的:
var key1 = "whatever";
var other_key = "blah";
var key2 = "";
for (var i = 0; i < key1.length; ++i)
{
var ch = key1.charCodeAt(i);
ch ^= other_key.charAt(i % other_key.length);
key2 += String.fromCharCode(ch);
}
但是,他们给出了不同的答案......
有什么问题,JavaScript 字符串的编码方式不同,我该如何纠正它们?