我正在测试我从谷歌搜索得到的下面的 VB 函数。我打算用它来生成哈希码以进行快速字符串比较。但是,有时两个不同的字符串具有相同的哈希码。例如,这些字符串
“122Gen 1 堆大小(.NET CLR 内存 w3wp):mccsmtpteweb025.20833333333333E-02”
“122Gen 2 堆大小(.NET CLR 内存 w3wp):mccsmtpteweb015.20833333333333E-02”
具有相同的哈希码 237117279。
请告诉我: - 该功能有什么问题?- 我该如何解决?
谢谢
马丁
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As Any, src As Any, ByVal bytes As Long)
Private Function HashCode(Key As String) As Long
On Error GoTo ErrorGoTo
Dim lastEl As Long, i As Long
' copy ansi codes into an array of long'
lastEl = (Len(Key) - 1) \ 4
ReDim codes(lastEl) As Long
' this also converts from Unicode to ANSI'
CopyMemory codes(0), ByVal Key, Len(Key)
' XOR the ANSI codes of all characters'
For i = 0 To lastEl - 1
HashCode = HashCode Xor codes(i) 'Xor'
Next
ErrorGoTo:
Exit Function
End Function