我很难将此代码段转换为VB.NET
Function DecryptPassword(ByVal s As String) As String
Dim i As Integer
Dim sPass As String = s.Trim()
For i = 1 To Len(sPass)
If Asc(Mid$(sPass, i, 1)) - 5 < 124 Then
'this line throws "type char $ does not match declared data type char"
Mid$(sPass, i, 1) = Chr$(Asc(Mid$(sPass, i, 1)) - 5)
Else
Mid$(sPass, i, 1) = Mid$(sPass, i, 1)
End If
Next
DecryptPassword = UCase(sPass) ' Convert UserPassword to UpperCase
End Function
它在VB6中运行良好但在VB.Net时抛出错误..
答案 0 :(得分:5)
试试这个版本:
Function DecryptPassword(ByVal s As String) As String
If String.IsNullOrEmpty(s) Then
Return String.Empty
End If
Dim sbPass As New System.Text.StringBuilder(s.Length)
For Each oCharacter As Char In s.Trim
If Asc(oCharacter) - 5 < 124 Then
sbPass.Append(Convert.ToChar(Asc(oCharacter) - 5))
Else
sbPass.Append(oCharacter)
End If
Next
Return sbPass.ToString.ToUpper
End Function
答案 1 :(得分:3)
这似乎奏效了(就像Booji Boy写的那样删除了“$”)。
Function DecryptPassword(ByVal s As String) As String
Dim i As Integer
Dim sPass As String = s.Trim()
For i = 1 To Len(sPass)
If Asc(Mid(sPass, i, 1)) - 5 < 124 Then
Mid(sPass, i, 1) = Chr(Asc(Mid(sPass, i, 1)) - 5)
Else
Mid(sPass, i, 1) = Mid(sPass, i, 1)
End If
Next
DecryptPassword = UCase(sPass) ' Convert UserPassword to UpperCase
End Function
答案 2 :(得分:0)
只是为了它,一个linq方法。
Function DecryptPassword(ByVal s As String) As String
Return s.Aggregate(Of String)(String.Empty, Function(acc, c) acc & Char.ToUpper(If(Asc(c) - 5 < 124, Convert.ToChar(Asc(c) - 5), c)))
End Function
或者
Function DecryptPassword(ByVal s As String) As String
Return New String((From c In s Select Char.ToUpper(If(Asc(c) - 5 < 124, Convert.ToChar(Asc(c) - 5), c))).ToArray)
End Function