<p>How can a <code>sdbm</code> hash function (such as <a href="http://www.cse.yorku.ca/~oz/hash.html" rel="nofollow noreferrer">this</a>) be implemented in C# ?</p>

[{'answer_id': 15971, 'author': 'Timbo', 'author_id': 1810, 'author_profile': 'https://Stackoverflow.com/users/1810', 'pm_score': 2, 'selected': False, 'text': '<p>You can take the C code almost without changes:</p>\n\n<pre><code>uint sdbm( string str )\n{\n    uint hash = 0;\n    foreach( char ch in str )\n    {\n        hash = ch + (hash &lt;&lt; 6) + (hash &lt;&lt; 16) - hash;\n    }\n    return hash;\n}\n</code></pre>\n\n<p>Or did you think of something more sophisticated?</p>\n'}, {'answer_id': 15972, 'author': 'ICR', 'author_id': 214, 'author_profile': 'https://Stackoverflow.com/users/214', 'pm_score': 1, 'selected': False, 'text': "<p>I don't have a C compiler set up so I can't test to see if it performs the same, but I <em>think</em> the following is correct:</p>\n\n<pre><code>private static ulong SBDM(string str)\n{\n    ulong hash = 0;\n\n    foreach (char c in str)\n    {\n        hash = c + (hash &lt;&lt; 6) + (hash &lt;&lt; 16) - hash;\n    }\n\n    return hash;\n}\n</code></pre>\n\n<p>If you just need to get a hash of the string and it doesn't matter too much what the implementation is you can always do the <code>String.GetHashCode()</code>;</p>\n"}, {'answer_id': 18111, 'author': 'Christo', 'author_id': 1658, 'author_profile': 'https://Stackoverflow.com/users/1658', 'pm_score': 0, 'selected': False, 'text': '<p>The result from the hash differs between the C++ and C# implementation. I figured out that str parameter needs to be passed as a byte array.</p>\n\n<pre><code>private uint sdbm(byte[] str)\n{\n    uint hash = 0;\n\n    foreach (char ch in str)\n        hash = ch + (hash &lt;&lt; 6) + (hash &lt;&lt; 16) - hash;\n\n    return hash;\n}\n</code></pre>\n\n<p>Call the method by converting the value to be hashed with the <code>BitConverter.GetBytes</code> method.</p>\n\n<pre><code>uint Hash = sdbm(BitConverter.GetBytes(myID));\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/15954', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1658/']