2012-10-13 51 views
1

我想轉換一個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 ""; 
    } 

我無法找到爲什麼它沒有回到正確的輸出

+0

看起來你錯過了'TempChar = Convert.ToChar(substr) ;' - 你需要分配字符的ASCII值。試試這個:'value =(int)TempChar;'。 – Tim

+0

thnx用於幫助我的朋友 – Annie

回答

2

試試這個:

VB.Net to C# Conversion

public string DecryptString(string EncryptedString) 
{ 
    string functionReturnValue = null; 

    string TempLine = null; 
    string TempChar = null; 
    string FinalStr = null; 
    int i = 0; 
    int value = 0; 

    FinalStr = null; 
    value = 0; 

    if (!string.IsNullOrEmpty(EncryptedString)) { 
     for (i = 1; i <= Strings.Len(EncryptedString); i++) { 
      try { 
       TempChar = Strings.Mid(EncryptedString, i, 1); 
       value = Strings.Asc(TempChar); 
       value = (value - (120)); 
       FinalStr = FinalStr + Strings.Chr(value); 
      } catch { 
       functionReturnValue = ""; 
       return functionReturnValue; 
      } 
     } 
     functionReturnValue = FinalStr; 
     return functionReturnValue; 
    } 

    functionReturnValue = ""; 
    return functionReturnValue; 
} 

參見:

1)Strings.Asc Method (String):返回表示對應於字符的字符碼的整數值。 (命名空間:Microsoft.VisualBasic程序)

2)What's the equivalent of VB's Asc() and Chr() functions in C#?

對於升序(),就可以把字符爲int這樣的:

int i = (int)your_char; 

和CHR()你可以像這樣從一個int轉換回char:

char c = (char)your_int; 
+0

OP將需要添加對Microsoft.VisualBasic程序集的引用以使用該方法。 [字符串.Asc方法(字符串)](http://msdn.microsoft.com/en-us/library/78sb8546.aspx) – Tim

2

我冒昧地清理代碼,因爲它更容易找到e rrors這樣:

Public Function DecryptString(ByVal EncryptedString As String) As String 

    Dim FinalStr As New StringBuilder 
    Dim value As Integer = 0 

    If not String.IsNullOrEmpty(EncryptedString) Then 
     For Each c as Char in EncryptedString 
      Try 
       value = Asc(c) 
       value -= 120 
       FinalStr.Append(Chr(value)) 
      Catch 
       Return "" 
      End Try 
     Next 
    End If 

    Return Finalstr.ToString 

End Function 

而作爲C#版本:

public string DecryptString(string EncryptedString) { 

    StringBuilder FinalStr = new StringBuilder(); 
    int value = 0; 

    if (!string.IsNullOrEmpty(EncryptedString)) { 

     foreach (char c in EncryptedString) { 
      try { 
       value = (int)c; 
       value -= 120; 
       FinalStr.Append(((char)(value))); 
      } 
      catch (System.Exception e) { 
       return ""; 
      } 
     } 
    } 
    return Finalstr.ToString; 
} 

看看是否能爲你工作。

+0

對於使用StringBuilder :) – Tim

+0

您的代碼也無法正常工作。結果字符串顯示]̤ɤ字符代替=符號。無論如何thnx – Annie

+0

我不知道你在解碼什麼,我只是用你的公式來做到這一點:-) – K3N

1

問題是,你輸(從來沒有使用)TempChar

如果你只是讓這種變化,你回家:

value = (TempChar - 120); 
+1

我不認爲這會奏效 - 你需要將'TempChar'強制轉換爲int。 – Tim

+0

適合我。我以爲我必須演員,但沒有。 – mawiks

+0

我會很沉默。幕後必須有隱含的表演。 – Tim

0

你永遠分配角色來value價值,所以你要與-120拉閘,-240等

這裏的另一種方式做到這一點,沒有所有的字符串操作:

if (!String.IsNullOrEmpty(EncryptedString)) 
{ 
    for (i = 0; i < EncryptedString.Length; i++) 
    { 
     try 
     { 
      TempChar = EncryptedString[i]; 
      value = (int)TempChar - 120; 
      FinalStr = FinalStr + (char)(value); 
     } 
     catch (System.Exception e) 
     { 
      // TODO: Exit Function: Warning!!! Need to return the value 
      return ""; 
     } 
    }