I've already asked this question on several forums, but without any good explanation of why the code above cannot be converted from C# to Visual Basic.
The code is actually from this forum, written in 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);
}
So, the code ought to be something like that in 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
It seems to be right, but it does not work. In VB.NET, there is an overflow at "hash += (hash << 10)"