传统的vbscript到c#等价

时间:2013-09-03 01:08:01

标签: c# asp.net encryption hash vbscript

我在经典的asp网站中继承了一个旧的Hashing算法,我将转换为asp.net(现阶段为2.0)。

对于我的生活,我无法理解旧函数,足以能够在C#中编写匹配的代码。我确信这很简单,但我现在看不到树林里的树林了。

以下是带有字符串的原始经典asp代码,对于无提示C#代码的任何帮助都将非常感激:

Function PHash( pValue )
           Dim dValue 
           Dim dAccumulator
           Dim lTemp 
           Dim sValue
           sValue = UCase(Trim("" & pValue))
           dAccumulator = 0
           For lTemp = 1 to Len(sValue)
              dValue = Asc(Mid(sValue, lTemp, 1))
              If (lTemp AND 1) = 1 Then
                 dAccumulator = Sin( dAccumulator  + dValue )
              Else
                 dAccumulator = Cos( dAccumulator  + dValue )
              End If
           Next
           dAccumulator = dAccumulator * CLng(10 ^ 9)
           PHash = CLng(dAccumulator)
End Function

3 个答案:

答案 0 :(得分:2)

我真的希望你有一两个参考测试,但这是我能想到的最好的

    private static long PHash(String pValue)
    {
        double dAccumulator = 0;
        byte[] asciiBytes = Encoding.ASCII.GetBytes(pValue.Trim().ToUpper());
        for (int i = 0; i < asciiBytes.Length; i++)
        {
            if ((i & 1) == 1)
                dAccumulator = Math.Cos(dAccumulator + (double)asciiBytes[i]);
            else
                dAccumulator = Math.Sin(dAccumulator + (double)asciiBytes[i]);
        }
        dAccumulator = dAccumulator * Math.Pow(10,9);
        return (long)dAccumulator;
    }

没有理由进行直接翻译,因为它涉及很多浪费。我将所有字符串解析逻辑替换为字节数组,然后使用for循环进行迭代。我们使用&运算符在VB中复制AND运算符。 SinCos现在是Math类的方法,可以通过链接方法Trim()ToUpper()来修剪字符串并将其转换为大写。 .NET中没有指数运算符,因此Math.Pow()替代它。请注意,我们将所有内容保持为双倍直到返回行,我们将值返回为long

答案 1 :(得分:0)

您可以使用此代码。请参阅转化工具:http://www.developerfusion.com/tools/convert/vb-to-csharp/?batchId=dbded81a-54c9-4df5-aced-4db45773c842

  public object PHash(pValue)
  {
   dynamic dValue = null;
   dynamic dAccumulator = null;
   dynamic lTemp = null;
   dynamic sValue = null;
   sValue = Strings.UCase(Strings.Trim("" + pValue));
   dAccumulator = 0;
   for (lTemp = 1; lTemp <= Strings.Len(sValue); lTemp++) {
    dValue = Strings.Asc(Strings.Mid(sValue, lTemp, 1));
    if ((lTemp & 1) == 1) {
        dAccumulator = Sin(dAccumulator + dValue);
    } else {
        dAccumulator = Cos(dAccumulator + dValue);
    }
}
 dAccumulator = dAccumulator * Convert.ToInt64(Math.Pow(10, 9));
 return Convert.ToInt64(dAccumulator);
 }

答案 2 :(得分:-1)

翻译可能是这样的:

public long PHash (string pValue)
{
    int dValue;
    double dAccumulator;
    int lTemp;
    string sValue;

    sValue = pValue.Trim().ToUpper();
    dAccumulator = 0;

    for (lTemp = 1; lTemp <= sValue.Length; lTemp ++)
    {
        dValue = (int)char.Parse(sValue.Substring(lTemp, 1));
        if ((lTemp % 1) == 1)
        {
            dAccumulator = Math.Sin(dAccumulator  + dValue);
        }
        else
        {
            dAccumulator = Math.Cos(dAccumulator  + dValue);
        }
    }
    dAccumulator = dAccumulator * (long)(10 ^ 9);
    return (long)(dAccumulator);
}

翻译的功能是: 修剪:删除字符串左侧和右侧的空格。在C#中:Trim()。 UCase:将指定的字符串转换为大写。在C#中:ToUpper()。 Mid:从字符串返回指定数量的字符。在C#:Substring()中。 Len:返回字符串中的字符数。在C#中:长度。 CLng:对长型是简单的演员。 Asc:被解析为从char中简单地转换为int。