Eric,
Do you want a string as the result?
I don't think I would bother with a BitArray, instead use a normal Char
array, iterate over both strings xor each char into this char array, then I
would create a new string based on the array:
Something like:
' VS.NET 2003 syntax
Option Strict On
Option Explicit On
Private Function XorStrings(ByVal s1 As String, ByVal s2 As String) As
String
' length of the resulting string
Dim length As Integer = Math.Max(s1.Length, s2.Length)
' ensure both strings are the same length
s1 = s1.PadRight(length, Char.MinValue)
s2 = s2.PadRight(length, Char.MinValue)
' xor one string into the other
Dim chars(length - 1) As Char
For index As Integer = 0 To length - 1
chars(index) = ChrW(AscW(s1.Chars(index)) Xor
AscW(s2.Chars(index)))
Next
' return a new string, eliminating trailing padding
Dim result As New String(chars)
Return result.TrimEnd(Char.MinValue)
End Function
Public Sub Main()
Dim s1 As String = "First String"
Dim s2 As String = "Second string"
Dim result As String = XorStrings(s1, s2)
result = XorStrings(result, s2)
End Sub
Instead of Char.MinValue you could a different char to pad the string length
to in XorStrings, I would use the same string to trim the end.
Note the above does 16 bit char values, if instead you want ASCII or ANSI
values (bytes), I would use System.Text.Encoding to convert the string to a
byte array, then xor each byte in both arrays. Optionally you could use
System.Text.Encoding to convert the byte arrays back to a String.
Thus, my question is this: is there an easy way to convert a String to a
BitArray (and back again)?
Use the System.Text.Encoding class to decode the string into an array of
Bytes, pass this to the BitArray constructor. To get back to a string, use
BitArray.CopyTo to copy the contents of the BitArray to an array of bytes,
then use the System.Text.Encoding class to convert this array of bytes back
to a string.
Hope this helps
Jay