XOR-ing strings in C#
c#, xor
Solution
You are doing an `xor` on 2 characters. This will do an implicit type conversion to `int` for you since there is no data loss. However, converting back from `int` to `char` will need you to provide an explicit cast.
You need to explicitly convert your `int` to a `char` for `result[i]`:
result[i] = (char) (charAArray[i] ^ charBArray[i]);
Problem
I recently started playing around with C#, and I'm trying to understand why the following code doesn't compile. On the line with the error comment, I get: Cannot implicitly convert type 'int' to 'char'. An explicit conversion exits (are you missing a cast?) I'm trying to do a simple XOR operation with two strings. ``` public string calcXor (string a, string b) { char[] charAArray = a.ToCharArray(); char[] charBArray = b.ToCharArray(); char[] result = new char[6]; int len = 0; // Set length to be the length of the shorter string if (a.Length > b.Length) len = b.Length - 1; else len = a.Length - 1; for (int i = 0; i < len; i++) { result[i] = charAArray[i] ^ charBArray[i]; // Error here } return new string (result); } ```