XOR and other things

TinaP

Well-known Member
Joined
Jan 26, 2005
Messages
528
I recently acquainted myself with the XOR operator in VBA and have found it very useful over the past couple days, which got me wondering...

1) Does anyone else use the XOR operator? and,
B) Are there any operators/statements/functions that you've learned belatedly?

Disclaimer: I realize I could have used the OR function, but in the project I was working on, I would have needed two more lines of code and nested IF statements; so don't judge me. ;)
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
I believe I implemented XOR in a solution once but forget what that was (I might still be using it for all I know - I have a lot of code running now).

Other items I discovered belatedly:
Name ... As ... --> rename a file
Dim var As String * 20 --> declared a fixed width string
 
Last edited:
Upvote 0
If A and B are numbers then (A Xor B) Xor B = A
Assuming B is ASC code of the (next) char in the password, the Xor can be used in password encryption / decryption.
Like this:
Rich (BB code):
Sub XorUsage()
 
  Dim Pwd As String
 
  ' Put into A1 the text to be encrypted /decrypted
  [A2:A4].ClearContents
  If Len([A1].Value) = 0 Then [A1].Value = "The quick brown fox jumps over the lazy dog"
 
  ' Set the password
  Pwd = InputBox("Type the password", [A1].Value, "My Password")
  If Len(Pwd) = 0 Then Exit Sub
 
  ' Put encrtypded A1 to A2
  [A2].Value = StrXor([A1].Value, Pwd)
   
  ' Put Decrypted A2 to A3
  [A3].Value = StrXor([A2].Value, Pwd)
   
  ' Compare Decrypted A3 with Original A1
  [A4].Formula = "=A1=A3"
 
End Sub
 
 
Function StrXor(Txt As String, Pwd As String) As String
  Dim a As Integer, b As Integer, c As Integer, i As Long, j As Long
  For i = 1 To Len(Txt)
    j = j + 1
    If j > Len(Pwd) Then j = 1
    a = Asc(Mid(Txt, i))
    b = Asc(Mid(Pwd, j))
    c = a Xor b   ' <-- Encription/Decription
    c = c Xor 255 ' <-- This excludes Chr(0)
    StrXor = StrXor & Chr(c)
  Next
End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,213,511
Messages
6,114,054
Members
448,543
Latest member
MartinLarkin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top