4

Is there some formal standards that do not allow one to use the ^ or XOR function in C# with two chars?

    public char[] XORinput(char[] input, char SN)
    {
        int InputSize = input.Length;
        //initialize an output array
        char[] output = new char[InputSize];

        for (int i = 0; i < InputSize; i++)
        {

            output[i] = input[i] ^ SN;

        }


        return output;

    }

For whatever reason this code is giving me this error, Error 1400 Cannot implicitly convert type 'int' to 'char'. An explicit conversion exists (are you missing a cast?)

This does not make any sense.

Caller:

                string num = "12345";
                char SN = Convert.ToChar(num);//serial number

                string sCommand = ("Hellow");

                char[] out = new char[sCommand.ToCharArray().Length];


                out = calculator.XORinput(sCommand.ToCharArray(), SN);
Recurrsion
  • 231
  • 3
  • 13
  • What does the caller look like? – Matthew Jul 19 '12 at 18:08
  • I see the problem, num is not a char, its a char array – Recurrsion Jul 19 '12 at 18:16
  • Perhaps you should modify your function to work with bytes / shorts instead. `char` is 16bit, which is fine on its own, but it is meant to hold UTF-16 non-surrogate characters, you could potentially fill it with a value that cannot be represented by UTF-16. This could cause problems if you ever try and render that character, if you're never rendering it, then why have a char in the first place? – Matthew Jul 19 '12 at 18:24
  • In your conversion from string to char array, this is the way to do it: `char[] out = sCommand.ToCharArray(0,sCommand.Length);` – roymustang86 Jul 19 '12 at 18:47

3 Answers3

7

The error is not with the function, but with the result of it.

When you do input[i] ^ SN; your result is an int, which you "Cannot implicitly convert type 'int' to 'char'.

You can cast it like this:

(char)(input[i] ^ SN);
NominSim
  • 8,447
  • 3
  • 28
  • 38
  • Why does `char ^ char` return `int`, though? – StriplingWarrior Jul 19 '12 at 18:17
  • 6
    Because certain operations, such as arithmetic and XOR, are only defined on `int`s in .NET. The `char`s are implicitly converted to `int`s (so now it's `int ^ int`) so that they can be XORed, which results in an `int` result. – Tim S. Jul 19 '12 at 18:20
  • @TimS.: Thanks for the response. Further reading: http://stackoverflow.com/a/942349/120955 – StriplingWarrior Jul 19 '12 at 18:26
4

The xor operator (along with other operators) return an integer result. So a single cast to char is sufficient.

output[i] = (char)(input[i] ^ SN);

In this case you wouldn't have to cast, but it's less efficient in your case:

output[i] = input[i];
output[i] ^= SN;
huysentruitw
  • 27,376
  • 9
  • 90
  • 133
1

If you have a character, a char, you can convert it to an integer, an int.

And then you can use the ^ operator to perform XOR on it. You don't appear to be using that operator at the moment, which might be the source of your problem.

    output[i] = (char)((uint)input[i] ^ (uint)SN);
roymustang86
  • 8,054
  • 22
  • 70
  • 101