Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SynphonyKnight · Nov 11, 2016 at 08:33 AM · c#stringintgethashcode

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!

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image doublemax · Nov 11, 2016 at 10:15 PM 0
Share

Just use your own hash algorithm, if you want a 32bit int as a result, a simple CRC32 should to the trick.

avatar image SynphonyKnight · Nov 13, 2016 at 02:34 AM 0
Share

Thanks for the advice. It took forever, but I made my own algorithm for this. Thanks!

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by quarzwar · Mar 30, 2020 at 02:33 PM

I know this is an old post but uhm...

string.GetStableHashCode();

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SynphonyKnight · Mar 30, 2020 at 05:37 PM 0
Share

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?

avatar image sacredgeometry SynphonyKnight · Mar 30, 2020 at 05:56 PM 0
Share

Sorry I misread. Try this.

https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.md5?view=netframework-4.8

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges