This is an equivalent implementation of .NET's String.GetHashCode in "safe" C# and ES5 JavaScript. If you write your own version in another language, feel free to comment below!
- The implementation (and therefore the generated hashcode) varies depending on the version of .NET that the app was compiled with. Here I provide .NET 3.5 and .NET 4.0, although it's likely that other versions work differently.
- In .NET 4.0,
GetHashCode
will often overflow the 32-bit signed integer limit. Other languages often use double-precision floating point values to represent all numbers, and will need to implement 32-bit signed integer overflow to mimic this. - Be mindful of accuracy when multiplying large numbers. For example in C#
782507206 + 1369868898 * 1566083941
is2145329683215674224
, but in JavaScript it's represented as2145329683215674400
.
Input | .NET 3.5 | .NET 4.0 |
---|---|---|
"foo" |
101574 | 1502598398 |
"foobar" |
-1268878963 | 836807663 |
"the quick brown fox jumped over the lazy dog" |
-772038699 | -1284118495 |