Given two strings as input, return the result of XORing the code-points of one string against the code points of the other.
For each character in the first input string, take the code-point (e.g. for A, this is 65) and XOR the value against the corresponding index in the second string and output the character at the code-point of the result. If one string is longer than the other, you must return the portion of the string beyond the length of the shorter, as-is. (Alternatively, you may pad the shorter string with NUL bytes, which is equivalent.)
See the following JavaScript code for an example:
const xorStrings = (a, b) => {
let s = '';
// use the longer of the two words to calculate the length of the result
for (let i = 0; i < Math.max(a.length, b.length); i++) {
// append the result of the char from the code-point that results from
// XORing the char codes (or 0 if one string is too short)
s += String.fromCharCode(
(a.charCodeAt(i) || 0) ^ (b.charCodeAt(i) || 0)
);
}
return s;
};
Try it online!
Test cases
Input Output
['Hello,', 'World!'] '\x1f\x0a\x1e\x00\x0b\x0d'
['Hello', 'wORLD'] '?*> +'
['abcde', '01234'] 'QSQWQ'
['lowercase', "9?' "] 'UPPERCASE'
['test', ''] 'test'
['12345', '98765'] '\x08\x0a\x04\x02\x00' _not_ 111092
['test', 'test'] '\x00\x00\x00\x00'
['123', 'ABCDE'] 'pppDE'
['01', 'qsCDE'] 'ABCDE'
['`c345', 'QQ'] '12345'
Rules
- The two input strings will only ever be code-points 0-255.
- This is code-golf so the shortest solution, in each language, wins.
$args[1]|% t*yinstead[char[]]($args[1]). The% t*ymeans "call the methodtoCharArray. And you can omit$i=0. See also Tips for golfing in PowerShell \$\endgroup\$Iandaminstead ofI am fatandYou are thin. 2. As you can see in your result, it contains only 1 byte instead of two bytes (amhas two bytes), because you process up to the length of the first string. In conclusion, you just omit bytes if the first string is the shorter one and your code crashes if the second string is shorter. \$\endgroup\$