2012-11-21 52 views
-3

我需要在C#以下邏輯:需要以下VB邏輯在C#

Dim sampleText As String, Key As String; 
Dim KeyLength As Long, Position As Long; 
Dim character as integer; 

For Position = 1 To Len(sampleText) 
     character = Asc(Mid$(Key, (Position Mod KeyLength) - KeyLength * ((Position Mod KeyLength) = 0), 1)) 
     Mid$(sampleText, Position, 1) = Chr(Asc(Mid$(sampleText, Position, 1)) Xor character) 
    Next Position 
+0

你試過了什麼? –

+0

cmon不要被這個懶惰 – sajanyamaha

+0

我面臨的主要問題是以下行: character = Asc(Mid $(Key,(Position Mod KeyLength) - KeyLength *((Position Mod KeyLength)= 0),1)) 最後我對'= 0'有懷疑。 –

回答

0

在VB中的 '中秋節' 語句沒有直接的等價物(不同於'Mid'功能):

string sampleText = null; 
string Key = null; 
long KeyLength = 0; 
long Position = 0; 
int character = 0; 

for (Position = 1; Position <= sampleText.Length; Position++) 
{ 
    character = Convert.ToInt32(Key[(Position % KeyLength) - KeyLength * ((Position % KeyLength) == 0) - 1]); 
    SimulateMidStatement.MidStatement(sampleText, Position, (char)(Convert.ToInt32(sampleText[Position - 1])^character), 1); 
} 

//---------------------------------------------------------------------------------------- 
// Copyright © 2003 - 2012 Tangible Software Solutions Inc. 
// This class can be used by anyone provided that the copyright notice remains intact. 
// 
// This class simulates the behavior of the classic VB 'Mid' statement. 
// (Note that this is unrelated to the VB 'Mid' function). 
//---------------------------------------------------------------------------------------- 
public static class SimulateMidStatement 
{ 
    public static void MidStatement(ref string target, int oneBasedStart, char insert) 
    { 
     if (target == null) 
      return; 

     target = target.Remove(oneBasedStart - 1, 1).Insert(oneBasedStart - 1, insert.ToString()); 
    } 
    public static void MidStatement(ref string target, int oneBasedStart, string insert) 
    { 
     if (target == null || insert == null) 
      return; 

     target = target.PadRight(target.Length + insert.Length).Remove(oneBasedStart - 1, insert.Length).Insert(oneBasedStart - 1, insert).Substring(0, target.Length); 
    } 
    public static void MidStatement(ref string target, int oneBasedStart, string insert, int length) 
    { 
     if (target == null || insert == null) 
      return; 

     int minLength = System.Math.Min(insert.Length, length); 
     target = target.PadRight(target.Length + insert.Length).Remove(oneBasedStart - 1, minLength).Insert(oneBasedStart - 1, insert.Substring(0, minLength)).Substring(0, target.Length); 
    } 
} 
+1

Blimey!這是很多代碼!如果您需要Mid,只需導入Microsoft.VisualBasic並寫入String.Mid。任務完成!它是.Net框架的完全支持部分。 – MarkJ

+0

如果你選擇3所需的重載,那麼它不是那麼糟糕,但你是正確的 - 你可以導入Microsoft.VisualBasic。 –

3

你去那裏:

C#

character = Strings.Asc(Strings.Mid(Key, (Position % KeyLength) - KeyLength * ((Position % KeyLength) == 0), 1)); 
Strings.Mid(sampleText, Position, 1) = Strings.Chr(Strings.Asc(Strings.Mid(sampleText, Position, 1))^character); 
+0

+1只是導入Microsoft.VisualBasic。任務完成! – MarkJ