根据每个字符替换前后的字符的 ASCII 码即可找到规律,即奇数 ASCII 码时与该 ASCII 码的下一个 ASCII 码所对应的字符替换,参考代码如下:
' 模块代码
Option Explicit
Public Function ReplaceEx(ByVal Target As String) As String
Target = UCase$(Target)
Dim intLoop As Integer
Dim intASCII As Integer
For intLoop = 1 To Len(Target)
intASCII = Asc(Mid$(Target, intLoop, 1))
Select Case intASCII
' 0-9, A-Z
Case 48 To 57, 64 To 90
If intASCII Mod 2 = 1 Then
Mid$(Target, intLoop, 1) = Chr$(intASCII - 1)
Else
Mid$(Target, intLoop, 1) = Chr$(intASCII + 1)
End If
End Select
Next
ReplaceEx = Target
End Function
' 窗体按钮 Click 事件代码
Private Sub Command1_Click()
MsgBox ReplaceEx("?AcKR18"), vbInformation, "ReplaceEx"
End Sub
输出:
![](https://video.ask-data.xyz/img.php?b=https://iknow-pic.cdn.bcebos.com/8b13632762d0f70373cfe2e501fa513d2697c58e?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_600%2Ch_800%2Climit_1%2Fquality%2Cq_85%2Fformat%2Cf_auto)
补充说明:
ReplaceEx 函数中使用了 UCase$ 函数忽略了输入参数 Target 的大小写,如果要严格区分那就去掉 Target = UCase$(Target)
追问没有对! 比如我输入E9N0CV 输出就是D8O1BW
追答![](https://video.ask-data.xyz/img.php?b=https://iknow-pic.cdn.bcebos.com/203fb80e7bec54e7269e4827b0389b504fc26a9c?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_600%2Ch_800%2Climit_1%2Fquality%2Cq_85%2Fformat%2Cf_auto)
追问能不能把你这个存一个工程给我
追答如果你要输入时直接看到效果,不妨把对 ReplaceEx 的调用放入 TextBox 的键盘事件中
Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
Text2.Text = ReplaceEx(Text1.Text)
End Sub
工程窗体中上面一个文本框名称为 Text1,下面一个为 Text2
实时改变效果:
![](https://video.ask-data.xyz/img.php?b=https://iknow-pic.cdn.bcebos.com/1b4c510fd9f9d72abc2c4f24dd2a2834349bbbbb?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_600%2Ch_800%2Climit_1%2Fquality%2Cq_85%2Fformat%2Cf_auto)
追问晓得了哈 谢谢