Click here to Skip to main content
15,887,273 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello There.
I'm having a problem with the operator XOR in C# (Operator '^' cannot be applied to operands of type 'int' and 'double').
I don't know why I'm getting this error and I really hope that someone will be able to tell me what I'm doing wrong.

Note: I have made the code in VB and it works fine.

This is my code in C#:

C#
string key, Pass2;
string car, code;
int i,j;
key = "&@/.^$#!%ü";
Pass2 = "";
j = 1;
for (i = 1; i <= Pass.Length; i +=2)
{
    car = Strings.Mid(Pass, i, 2);
    code = Strings.Mid(key, ((j - 1) % key.Length) + 1, 1);

    Pass2 = Pass2 + Strings.Chr(Strings.Asc(code) ^ Conversion.Val("&h" + car)); //here's where I'm getting the error 
    j = j + 1;
}
return Pass2;


And this one in VB:

VB
Function DeCrypt(ByVal Pass As String) As String
    Dim Key As String, i As Integer, Pass2 As String
    Dim CAR As String, Code As String
    Dim j As Integer
    'Key = "%ü&/@#$A"
    Key = "&@/.^$#!%ü"
    Pass2 = ""
    j = 1
    For i = 1 To Len(Pass) Step 2
        CAR = Mid(Pass, i, 2)
        Code = Mid(Key, ((j - 1) Mod Len(Key)) + 1, 1)
        Pass2 = Pass2 & Chr(Asc(Code) Xor Val("&h" + CAR))
        j = j + 1
    Next i
    DeCrypt = Pass2
End Function
Posted
Updated 30-Jul-12 13:41pm
v2

1 solution

Assuming you are referencing the Microsoft.VisualBasic dll from your source code, which is not the best approach in my oppinion, you could modify the error line using:
C#
Pass2 = Pass2 + Strings.Chr(Strings.Asc(Code) ^ (int)Conversion.Val("&h" + CAR));

A clean c# solution would be:
C#
Pass2 = Pass2 + Strings.Chr(Convert.ToInt32(Code) ^ Convert.ToInt32("&h" + CAR));

Hope that helps
 
Share this answer
 
v2
Comments
EddyGuzman 30-Jul-12 20:03pm    
Thanks! it was the int...
5S for you
JF2015 30-Jul-12 20:04pm    
I'm glad that I could help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900