Question calculating xor of different characters

mmy

Member
Joined
Oct 5, 2007
Messages
24
Programming Experience
1-3
Hi,

for a project, I have to calculate a checksum that would be the XOR of every individual ascci character.

For example -> (string) "12".
-> Character 1 = Ascii 0x31 = binary 110001
-> Character 2 = Ascii 0x32 = binary 110010

0x31 XOR 0x32 = 0x003 (according to my windows calculator)
110001 XOR 110010 = 000011 (= 0x003) - this is also correct, since the last 2 digits are uneven resulting in binary value 1

But I can't seem to get this programmed in vb.net. I think there's a conversion somewhere, but I can't get it right... (I read the string "12" from a textbox)

I tried this:
VB.NET:
Dim result As String
result = StrToHex("1") Xor StrToHex("2")
MsgBox(result)
The messagebox returns "63", but I have no idea how this is achieved. Can I convert this to the hexadecimal value?
The StrToHex("1") returns "31" so, the conversion seems correct.


After this, I can try a for each block to cycle through each character from the textbox... Hope someone can help me with this issue...
 
hmm, seems this does the trick I think...
VB.NET:
dim result as string
result = Hex(CLng("1") Xor CLng("2"))  
'OR
result = Hex(AscW("1") Xor AscW("2"))
-> gives 3 as a result

But now I have to put this into a for each loop...
VB.NET:
For Each mychar As Char In "12"
result = Hex(AscW(result) Xor AscW(mychar))
Next
MsgBox(result)
This says that the argument string has to be greater then zero (but the first time, the result variable is empty...) Maybe I have to give a value containing all "1" (I mean binary 1111111 -> "value 1" XOR 1111111111 = value 1)
 
Last edited:
Back
Top