Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在VB.NET中实现GetStableHash方法_C#_Vb.net - Fatal编程技术网

C# 如何在VB.NET中实现GetStableHash方法

C# 如何在VB.NET中实现GetStableHash方法,c#,vb.net,C#,Vb.net,我已经在几个论坛上问过这个问题,但没有很好地解释为什么上面的代码不能从C#转换为Visual Basic 这段代码实际上来自这个论坛,是用C#编写的 static public int GetStableHash(字符串s) { uint散列=0; //如果你在意的话,用不安全的方法可以更快地完成 //使用固定字符*重新解释为字节* foreach(System.Text.Encoding.Unicode.GetBytes(s)中的字节b) { hash+=b; 散列+=(散列>6); } //

我已经在几个论坛上问过这个问题,但没有很好地解释为什么上面的代码不能从C#转换为Visual Basic

这段代码实际上来自这个论坛,是用C#编写的

static public int GetStableHash(字符串s)
{
uint散列=0;
//如果你在意的话,用不安全的方法可以更快地完成
//使用固定字符*重新解释为字节*
foreach(System.Text.Encoding.Unicode.GetBytes(s)中的字节b)
{
hash+=b;
散列+=(散列>6);
}
//最终雪崩
hash+=(hash>11);
散列+=(散列6)
下一个
散列+=(散列>11)

哈希+=(哈希溢出检查在C#中默认为关闭,但在VB.NET中默认为打开。Project+属性、编译选项卡、向下滚动、高级编译选项并勾选“删除整数溢出检查”选项


如果这让您感到不舒服,那么将代码移动到一个单独的类库项目中,这样设置更改就不会影响代码的其余部分

溢出检查在C#中默认为关闭,但在VB.NET中默认为打开。Project+属性、编译选项卡、向下滚动、高级编译选项并勾选“删除整数溢出检查”选项


如果这让您感到不舒服,那么将代码移动到一个单独的类库项目中,这样设置更改就不会影响代码的其余部分。另一个项目现在也可以是C#项目:)

正如Hans解释的那样,您会收到一个错误,因为VB正在进行溢出检查,而C#则不会。如果没有溢出检查,任何额外的biT被简单地丢弃。您可以通过在计算过程中使用更大的数据类型并手动丢弃多余的位来复制相同的行为。如果希望答案与C完全匹配,则需要额外的1行代码,或者额外的3行代码(查看注释):

公共共享函数GetStableHash(ByVal s作为字符串)作为整数
'使用64位整数而不是32位整数
Dim散列为ULong=0
对于System.Text.Encoding.Unicode.GetBytes(s)中的每个b作为字节
散列+=b
散列+=(散列>11)

散列+=(hash正如Hans所解释的,您会得到一个错误,因为VB正在进行溢出检查,而C#没有。如果没有溢出检查,任何额外的位都会被简单地丢弃。您可以通过在计算过程中使用更大的数据类型并手动丢弃额外的位来复制相同的行为。这需要额外的1行代码或3个l如果您希望答案与C完全匹配,请输入代码行(查看注释):

公共共享函数GetStableHash(ByVal s作为字符串)作为整数
'使用64位整数而不是32位整数
Dim散列为ULong=0
对于System.Text.Encoding.Unicode.GetBytes(s)中的每个b作为字节
散列+=b
散列+=(散列>11)

散列+=(散列无法更改VB.net中单个文件/部分文件的溢出检查?类似于C#
unchecked
关键字?不,语言中没有关键字。不可能,溢出检查是由语言语法假设的。这就是为什么不必强制转换以将int分配给字节的原因。无法更改吗在VB.net中对单个文件/文件的一部分进行溢出检查?类似于C#
未选中的
关键字?不,语言中没有关键字。不可能,溢出检查是由语言语法假设的。这就是为什么不必强制转换来为字节分配int。
static public int GetStableHash(string s)
       {
           uint hash = 0;
            // if you care this can be done much faster with unsafe 
            // using fixed char* reinterpreted as a byte*
            foreach (byte b in System.Text.Encoding.Unicode.GetBytes(s))
            {

                hash += b;
                hash += (hash << 10);
                hash ^= (hash >> 6);

            }
            // final avalanche
            hash += (hash << 3);
            hash ^= (hash >> 11);
            hash += (hash << 15);
            // helpfully we only want positive integer < MUST_BE_LESS_THAN
            // so simple truncate cast is ok if not perfect
            return (int)(hash % MUST_BE_LESS_THAN);
       }
    Const MUST_BE_LESS_THAN As Integer = 100000000

Function GetStableHash(ByVal s As String) As Integer


    Dim hash As UInteger = 0

    For Each b as Byte In System.Text.Encoding.Unicode.GetBytes(s)
        hash += b
        hash += (hash << 10)
        hash = hash Xor (hash >> 6)
    Next

    hash += (hash << 3)
    hash = hash Xor (hash >> 11)
    hash += (hash << 15)

    Return Int(hash Mod MUST_BE_LESS_THAN)
End Function
Public Shared Function GetStableHash(ByVal s As String) As Integer

  ' Use a 64-bit integer instead of 32-bit
  Dim hash As ULong = 0

  For Each b As Byte In System.Text.Encoding.Unicode.GetBytes(s)
     hash += b
     hash += (hash << 10)
     ' Throw away all bits beyond what a UInteger can store
     hash = hash And UInteger.MaxValue
     hash = hash Xor (hash >> 6)
  Next

  hash += (hash << 3)
  ' Throw away all extra bits
  hash = hash And UInteger.MaxValue
  hash = hash Xor (hash >> 11)
  hash += (hash << 15)
  ' Throw away all extra bits
  hash = hash And UInteger.MaxValue

  Return Int(hash Mod MUST_BE_LESS_THAN)
End Function