C# 需要以下c语言中的VB逻辑

C# 需要以下c语言中的VB逻辑,c#,vb.net,logic,C#,Vb.net,Logic,我需要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 Ke

我需要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
好了:

c


VB中的“Mid”语句与“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);
    }
}

我面临的主要问题是以下几行:character=AscMid$Key,Position Mod KeyLength-KeyLength*Position Mod KeyLength=0,1我对结尾的“=0”表示怀疑。这是非常弱的XOR加密。您可以查看.Net库中的一些更强大的加密。以下表达式如何计算为值:abc=position%keylength-keylength*position%keylength==0+1 just import Microsoft.VisualBasic。工作完成了!天哪!这是很多代码!如果需要Mid,只需导入Microsoft.VisualBasic并编写String.Mid即可。工作完成了!它是.Net framework的一个完全受支持的部分。如果您选择了3中所需的重载,那么它还不错,但您是正确的-您可以只导入Microsoft.VisualBasic。
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);
    }
}