0

我在一个经典的 asp 站点中继承了一个旧的哈希算法,我正在将其转换为 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
4

3 回答 3

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 运算符。Sin并且Cos现在是Math类的方法,可以通过链接方法Trim()和. 来修剪字符串并将其转换为大写ToUpper()。.NET 中没有指数运算符,因此Math.Pow()可以替代它。请注意,我们将所有内容都保留为双精度,直到返回行,我们将值作为long

于 2013-09-03T02:25:30.137 回答
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);
 }
于 2013-09-03T06:00:50.407 回答
-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);
}

翻译的函数是: Trim:删除字符串左右两侧的空格。在 C# 中:Trim()。UCase:将指定的字符串转换为大写。在 C# 中:ToUpper()。Mid:从字符串中返回指定数量的字符。在 C# 中:子字符串()。Len:返回字符串中的字符数。在 C# 中:长度。CLng:是对 long 类型的简单转换。Asc:被解析为从 char 到 int 的简单转换。

于 2013-09-03T02:00:37.477 回答