我有这个代码可以将字母表移动一定量。字母表的大小是 26。当我输入更大的尺寸时(例如 22),我会看到一些奇怪的字符。我想我需要将 ASCII 字母表修改为 26 才能使其正常工作,但我不太确定要修改哪个位。
基本上我需要环绕字母表(一旦到达 Z,它就会回到字母 A)我是否必须创建一个字典才能使 mod 工作(如 A = 0...Z = 26)或者我可以坚持使用正常的 ASCII 表?下面是代码:
Public Function encrypt(ByVal input As String) 'input is a variable within the funcion
Dim n as Integer
Dim i As Integer
n = key.Text Mod 26 'gets what is in the text box of 'key' and sets it as n
' the key is a multiple of 26 so 26 will = 0
'need to remove white spaces
While input.Contains(" ") 'when the input text contains a space
input = input.Replace(" ", "") 'replaces it with no space.
End While
For i = 1 To Len(input) 'find the length of the input
Mid(input, i, 1) = Chr(Asc(Mid(input, i, 1)) + n) 'chr returns the character associated with the specified character code
'
Next
encrypt = input
End Function