I'm trying to mimc the java HashCode method as shon below. . I’m not able to suppress overflowexception in VB so the value does’nt match exactly what I get in Java.

For example, hash'ing the string “test” and it matches java version. But hash “tester” and the 4-byte hash value overflows causing an exception in vb. And the resulting hashcode won't match what Java gives.

Public Function hashCode(ByVal data As String) As Integer
Dim Hash As Integer
Dim Length, Offset As Short

Hash = 0
Offset = 0
Dim val_Renamed() As Byte
Dim b As Byte
Dim tmp As Short
val_Renamed = System.Text.UnicodeEncoding.Unicode.GetBytes(data)
Length = UBound(val_Renamed)
Dim i As Short
For i = 1 To (Length / 2)
Hash = 31 * Hash + val_Renamed(Offset)
Catch e As System.OverflowException
End Try
Offset = Offset + 2
Next i
hashCode = Hash
End Function