我想轉換一個VB .NET函數的C#..我已經轉換,但預期它不返回輸出如何將vb .net函數轉換爲c#?
VB .NET功能
Public Function DecryptString(ByVal EncryptedString As String) As String
Dim TempLine As String = Nothing
Dim TempChar As String = Nothing
Dim FinalStr As String = Nothing
Dim i As Integer = 0
Dim value As Integer = 0
FinalStr = Nothing
value = 0
If EncryptedString <> "" Then
For i = 1 To Len(EncryptedString)
Try
TempChar = Mid(EncryptedString, i, 1)
value = Asc(TempChar)
value = (value - (120))
FinalStr = FinalStr & Chr(value)
Catch
DecryptString = ""
Exit Function
End Try
Next
DecryptString = FinalStr
Exit Function
End If
DecryptString = ""
End Function
C#功能
public string DecryptString(string EncryptedString)
{
string substr = null;
char TempChar; int ExtraChars = 0; string ExtraOnes = "";
string FinalStr = null;
int i = 0;
int value = 0;
FinalStr = null;
value = 0;
if ((EncryptedString != ""))
{
for (i = 1; (i <= EncryptedString.Length); i++)
{
try
{
substr = EncryptedString.Substring((i - 1), 1);
TempChar = Convert.ToChar(substr);
value = (value - 120);
FinalStr = (FinalStr + ((char)(value)));
}
catch (System.Exception e)
{
// TODO: Exit Function: Warning!!! Need to return the value
return "";
}
}
return FinalStr;
}
return "";
}
我無法找到爲什麼它沒有回到正確的輸出
看起來你錯過了'TempChar = Convert.ToChar(substr) ;' - 你需要分配字符的ASCII值。試試這個:'value =(int)TempChar;'。 – Tim
thnx用於幫助我的朋友 – Annie