What does the operator = mean in C#?

Asked

Viewed 352 times

14

I have a function in C#, where I decrypt a string and need to convert into a function in SQL Server for technicians to be able to work with the decrypted value.

There is a foreach, I don’t understand how it works:

var calculoChave = 0;
foreach (var c in chave)
{
    calculoChave ^= c;
}

What does this " =", and what it does?

Debugging the code, I realized that when the key has a number, it adds up to 48. Equal, if the key is equal to 3, the value of the key calculation will be 51. If the key has more than 2 numbers, change the calculation, and I did not find a logical sequence for this.

  • what type of variable chave? A String?

  • Do you want to know if there is a XOR operator in SQL? Or what it is ^=?

  • @Matthew The key is string yes.

  • @bigown would like to know what it is, to better understand the code. But since commented, I was doubtful if it exists in sql as well.

2 answers

10


  • Thanks for the explanation. I got it here.

  • I made a conversion to the ASCII table, which is the result of this expression.

  • I would like to know what is the mistake here to have a -1?

10

Only by completing the @Maniero response, how are you making one foreach about a String, means that you are traversing character by character from chave and performing a XOR between the variable value calculoChave and the decimal value of the current character, right after the result is stored in the variable itself calculoChave, for example:

chave = "ab"
caractere "a"; decimal = 97; binário = 0110 0001
caractere "b"; decimal = 98; binário = 0110 0010
valor 0 (zero);              binário = 0000 0000

Would look like this:

1ª iteração
0000 0000   (00)  0       calculoChave
0110 0001   (97) 'a'      caractere atual
---------   (XOR)
0110 0001   (97) 'a'      calculoChave = 97

2ª iteração
0110 0001   (97) 'a'      calculoChave
0110 0010   (98) 'b'      caractere atual
---------   (XOR)
0000 0011   (03)          calculoChave = 3

You can check the ascii table and look at the decimal and binary value of each character, note that there is a difference between the value 0 (zero) and the character "0" (zero).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.