[RESOLVED] Swap letters - sort of like a simple encryption
I was helping someone who wanted to replace capital letters in a string with their "opposites", eg Z to A, Y to B, X to C, etc. I came up with a way to do it by determining the Ascii value of the letters and using that value minus 64 as an index to get it's opposite via a hard-coded "ZYXW..." string. That works but I first tried to come up with a formula for converting the Ascii values of the letters (65 to 90) to their opposite, eg 90 to 65, 89 to 66, etc. but I was unsuccessful. Can anyone think of an algorithm for that that works?
Re: Swap letters - sort of like a simple encryption
155 - 90 = 65
155 - 89 = 66
155 - 88 = 67
....
Re: Swap letters - sort of like a simple encryption
Boy am I embarrased at the simplicity of that. Thanks.
Re: [RESOLVED] Swap letters - sort of like a simple encryption
Also, in general your function is linear, since going up some amount in the input causes some constant change in the output--go up 5 numbers in the input, go down 5 in the output. The relation must then be, for instance,
O = m*I + b,
for constants m and b. You can find them with some sample data. (I only mention this because it's a more general method to solve similar problems that occur pretty frequently. Otherwise, you're left with "intuit"ing the answer.)
Re: [RESOLVED] Swap letters - sort of like a simple encryption
Close but no cigar
Code:
Dim s As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Dim es As String
Dim c As Char
Dim i As Integer
For Each c In s
i = Asc(c) Xor 27
Debug.WriteLine(Chr(i))
Next