News for hackers. Stuff that matters.

Ask HN: What is wrong with my implementation of DJBX33X hashing function in C#?

I implemented DJBX33X hashing function in C# like this:

public static long hash(string str) {               long hash = 5381;     for (int i = 0; i < str.Length; i++)     {         hash = ((hash << 5) + hash) ^ (int)str[i];     }     return hash; } but if I compare results of my function with the following C implementation uint32_t hash(const char arKey, uint32_t nKeyLength) {     uint32_t hash = 5381;     for (; nKeyLength > 0; nKeyLength -=1)     {         hash = ((hash << 5) + hash) ^ arKey++;     }     return hash; } I get different results for the same input: C# t = 116(<<172192+177573) -> 177617 t = 116(<<5683744+5861361) -> 5861253 u = 117(<<187560096+193421349) -> 193421392 U = 85(<<6189484544+6382905936) -> 6382905861 'ttuU' => '6382905861'

C t = 116 (<<172192+177573) -> 177617 t = 116 (<<5683744+5861361) -> 5861253 u = 117 (<<187560096+193421349) -> 193421392 U = 85 (<<1894517248+2087938640) -> 2087938565 'ttuU' -> '2087938565'

( no comments | on HN )

There are no comments yet.