GetHashCode() in .NET 1.1 vs .NET 2.0

notepadd

When migrating GarageTV from .NET 1.1 to .NET 3.5 I ran in a “strange” problem.

It seems that there is a SearchBarrel table containing the links between keywords and posts (and some other info like keyword weight etc), so when a search is made you simply do a select using this table.

But… the keyword is hashed using String.GetHashCode() and that’s stored in the DB. Not clever it seems, because the GetHashCode() in .NET 2.0+ differs from the .NET 1.1 version.

Quote from MSDN: Prior to the .NET Framework version 2.0, the hash code provider was based on the System.Collections.IHashCodeProvider interface. Starting with version 2.0, the hash code provider is based on the System.Collections.IEqualityComparer interface.”

And for your information I’ve found an implementation of the GetHashCode() of the .NET 1.1 version, so you can use this method in .NET 2.0 projects.


Int32 HashString(String szStr)
{
ulong hash = 5381;

foreach (int ch in szStr)
{
hash = ((hash << 5) + hash) ^ (ulong)ch;
}

return (Int32)hash;
}

So… enjoy.

This entry was posted in Programming, Uncategorized and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>