Computers use numbers for characters. When you're doing a Caesar cipher on a computer, you have to get the number representations of the characters ["A","B","C","D"] to go from, say, [0,1,2,3]->[1,2,3,0], which looks like ["D","A","B","C"] if you regard "A" as being 0. So you can see that mostly you have to add 1 (in this case) to get the output value. However, if you add 1 to 3 you get 4, which is not one of the values you want. But! if you subtract 4 from the 1+3 then you get 0, which is what you want.
ASCII uses 65 for an "A", but you need it to be 0 for the maths to work (the [0,1,2,3] thing). So, if you get the ASCII value for "A" and subtract 65 then you get 0. Now you have something to work with mathematically. After the maths is done, you will have to add back the starting number (65 for uppercase letters) so that the character number corresponds with the representation of the character.
The other part of the ASCII character set you are probably concerned with is "a-z" (i.e. lowercase letters). You can do a similar thing with them, but there isn't really any need to know that Asc("a") is 97: you can get the computer to work that out for you by writing Asc("a")
.
As you are astute enough to realise that your repeated checks of the radio buttons could do with tidying up into one piece of code (part of what is called refactoring), I suspect that the following code might answer your question:
Function GetCaesarOffset() As Integer
Dim offset As Integer = -1
' put your RadioButton names here in order of 1, 2, ...
' where RadioButton1 represents a shift of 2 (no shift is a bit pointless)
Dim radButtons() As RadioButton = {RadioButton1, RadioButton2}
' iterate over the RadioButtons to find out which one is selected...
For i As Integer = 0 To radButtons.Length - 1
If radButtons(i).Checked Then
' we can return a value from the function right now
Return i + 1
End If
Next
Return -1 ' nothing was selected
End Function
Private Sub encrypt_btn_Click(sender As Object, e As EventArgs) Handles encrypt_btn.Click
Dim caesarOffset As Integer = GetCaesarOffset()
If caesarOffset = -1 Then
' the user has not chosen an offset value... tell them
MessageBox.Show("Please choose a value for the offset.")
' now we don't need to continue in this Sub
Exit Sub
End If
Dim txt As String = input.Text
Dim alphabetLength As Integer = Asc("Z") - Asc("A") + 1 ' this will usually be 26
Dim enciphered As String = ""
For i As Integer = 0 To txt.Length - 1
' get the ASCII character code (a number)
Dim c As Integer = Asc(txt(i))
' Check what range the ASCII code is in and take appropriate action
If c >= Asc("a") AndAlso c <= Asc("z") Then
' Look at lowercase letters
' make it into a number in the range 0-25 by subtracting the
' number which corresponds to the letter "a"
c = c - Asc("a")
c = c + caesarOffset
If c > alphabetLength Then
' oops! it has fallen off the end of the allowed values
' we can correct this by subtracting the length of the alphabet
c -= alphabetLength
End If
c = c + Asc("a")
enciphered &= Chr(c)
ElseIf c >= Asc("A") AndAlso c <= Asc("Z") Then
' Look at uppercase letters
c = c - Asc("A")
' we can use the Mod function instead of the If...Then
c = (c + caesarOffset) Mod alphabetLength
c = c + Asc("A")
enciphered &= Chr(c)
' you could put another ElseIf here for numbers if you wanted to
Else
' it wasn't an uppercase or lowercase character, so
' don't do anything to it.
enciphered &= txt(i)
End If
Next
output.Text = enciphered
End Sub
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…