- Home /
GetHashCode() questions/Turning a text string into an int
I was wondering what the best way to convert a text string into a int (which will be used as a seed)
I know GetHashCode() does this, but, at least according to this page, GetHashCode() is very unreliable in the fact that the value it creates may not be the same every time it is generated:
The hash code itself is not guaranteed to be stable. Hash codes for identical strings can differ across versions of the .NET Framework and across platforms (such as 32-bit and 64-bit) for a single version of the .NET Framework. In some cases, they can even differ by application domain.
Since this is used to generate a seed, I want it to always have the same number when, for example, the string is "Hello World". I want the number to be the same every time it is done, the same number on everyone's computer, always.
I'm unsure whether Unity's GetHashCode() will have the results I'm looking for or not. If you have more information on GetHashCode() that can help, I'll gladly take it.
If GetHashCode() won't work for what I want, I want to find a way to "numerize" a string of text into a number that is not too large for a regular 32-bit int. If anyone knows a method of doing so, I'll attempt to implement it.
Thank you!
Just use your own hash algorithm, if you want a 32bit int as a result, a simple CRC32 should to the trick.
Thanks for the advice. It took forever, but I made my own algorithm for this. Thanks!
Answer by quarzwar · Mar 30, 2020 at 02:33 PM
I know this is an old post but uhm...
string.GetStableHashCode();
As far as I could find on the official documentation, this method does not exist for the string class in the current version of C#/.NET, so I’m not sure where this is co$$anonymous$$g from. Either way, it’s fairly irrelevant because I found a workaround back a few years ago when I needed it. I am still interested in the answer you gave, however, because I’m curious of where you found it. Is that a string method that exists in another language and not C#, or am I missing something on the .NET documentation?
Sorry I misread. Try this.
https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.md5?view=netframework-4.8
Your answer
Follow this Question
Related Questions
Convert Text to float 3 Answers
Multiple Cars not working 1 Answer
Convert the user's input to an integer if it is equal to a key string from a different class. 0 Answers
Convert string to int C# 1 Answer
bool[string] = true; array possible? 2 Answers