Recentemente ho iniziato a giocare con C# e sto cercando di capire perché il seguente codice non viene compilato. In linea con il commento di errore, ottengo:Stringhe XOR in C#
Impossibile convertire in modo implicito il tipo 'int' in 'carattere'. Esce una conversione esplicita (ti manca un cast?)
Sto provando a eseguire un'operazione XOR semplice con due stringhe.
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);
}
Se la tua stringa è ASCII, prova questo 'result [i] = (char) ((short) charAArray [i]^(short) charBArray [i]);' – ebattulga
Gli operatori bitwise prendono solo numeri come operandi. La tua stringa è una serie di cifre numeriche? O stai provando a fare questa operazione sulla base dei valori ASCII? – JohnFx
Grazie, ho dimenticato che xor è un operatore bit a bit quindi impiega 1 e 0 ed è per questo motivo che si lamenta della conversione int. –