Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

XOR two strings

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 so the shortest solution, in each language, wins.

Answer*

Draft saved
Draft discarded
Cancel

AltStyle によって変換されたページ (->オリジナル) /