【发布时间】:2015-07-10 05:40:42
【问题描述】:
我已经在几个论坛上问过这个问题,但没有很好地解释为什么上面的代码不能从 C# 转换为 Visual Basic。
代码实际上来自这个论坛,用 C# 编写。 (the source)
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);
}
所以,代码应该是 VB.NET 中的代码
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
这似乎是对的,但它不起作用。在 VB.NET 中,"hash += (hash 处有溢出
【问题讨论】: