01-10-2006, 10:12 PM
Nice work! That looks alot better. While we're on the subject of functions, though, this may be a good time to show their usefulness. Additionally, you can use the string-indexing operator ( [] ) to crisp up that code even more:
Here, we solely work with the byte values of the strings, which is what operator[] gives us. As you may be able to guess, this is much more efficient than converting back and forth from strings to integers again and again.
Code:
option explicit
option byval
function CipherText( text as string, key as string ) as string
dim as integer textIndex = 0, keyIndex = 0
dim as string result = string( len( text ), 0 )
while( textIndex < len(text) )
if( keyIndex > len(key) ) then keyIndex = 0
result[textIndex] = text[textIndex] xor key[keyIndex]
textIndex += 1 : keyIndex += 1
wend
return result
end function
const originalText as string = "Move 32nd platoon SSW 10 miles"
const password as string = "armypassword"
dim as string cipheredText : cipheredText = CipherText( originalText,password )
print " original Text: " ; originalText
print " Password: " ; Password
print " Ciphered text: " ; cipheredText
dim as string decipheredText : decipheredText = CipherText( cipheredText,password )
print "Deciphered text: " ; decipheredText
sleep : end 0
stylin: