title: Why are the hash codes generated by this function not unique?
body: <p>I'm testing the VB function below that I got from a Google search.  I plan to use it to generate hash codes for quick string comparison. However, there are occasions in which two different strings have the same hash code. For example, these strings</p>
  
  <p>"122Gen 1 heap size (.NET CLR Memory w3wp):mccsmtpteweb025.20833333333333E-02"</p>
  
  <p>"122Gen 2 heap size (.NET CLR Memory w3wp):mccsmtpteweb015.20833333333333E-02"</p>
  
  <p>have the same hash code of 237117279.</p>
  
  <p>Please tell me:
  - What is wrong with the function?
  - How can I fix it?</p>
  
  <p>Thank you</p>
  
  <p>martin</p>
  
  <hr>
  
  <pre><code>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
  </code></pre>