• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

How does String to Integer work?

Status
Not open for further replies.
Level 8
Joined
Jul 29, 2010
Messages
319
Say I save a rect to a hashtable.
  • Hashtable - Save Handle OfRect <gen> as 1 of 1 in Hashtable1
If I save the second key as a string converted to an integer.
  • Hashtable - Save Handle OfLane 1 Portal <gen> as 1 of (Integer(Rect1)) in Hashtable1
How exactly is it converted to an integer? will it be saved as a value of 5 since there are 5 alphanumerics present? Thank you in advance to anyone who could explain how this works :)
 
It is pretty limited. It works well with strings that represent a number, like "50" or "2500", but when it sees a string that can't be converted to a number, like "Hello" or "A5432", it just gives 0. This is useful for parsing things like chat commands (e.g. '-gold 500'), so you can just take the chunk after "-gold" and give that player gold. If they put something invalid, like '-gold abcdefg', you would give them 0 gold.

For your use case, there is a JASS function called StringHash. It tries to convert the string into a unique number. So for example, StringHash("Hello") should give a different result than StringHash("Poops") (even though they are the same length!). The details of how it does this is a bit complex, but at a high-level:

  • Strings are ultimately just stored as raw bytes. A bunch of 1's and 0's.
  • The hash function loops through 12 bytes at a time, and does some magical math to mix the text together with some internal state. It then uses that result (plus the mixed up internal state) to process the next 12 bytes.
This analogy might help understand it:
  • Imagine each character in the string is a cooking ingredient, e.g. "A" -> Apple, "B" -> Banana, "X" -> ... Xantham gum, I guess.
  • You start off with 3 pots: the first one has carrots, the second one has carrots, the third has nothing. Why carrots? I don't know. But the chef decided he wanted to start off with carrots. The third pot is gonna be used to store your final product (the "hash" a.k.a. the number that represents our string).
  • You take 12 ingredients at a time. Put the first 4 ingredients into the first pot. Put the second 4 ingredients into the second pot. Put the last 4 ingredients into the third pot.
  • Now mix all of them together! (make sure you mix between pots too) You can do this however you'd like. Chop them up. Stick them in a blender. Throw them at the wall. Boil 'em. Mash 'em. Stick 'em in a stew. Anything.
  • After that "mixing" phase, you'll have a bunch of random mixed ingredients in pot #1, pot #2, and pot #3. So take the next batch of ingredients (i.e. the next characters in the string), and repeat that mixing you did in the last step! Note that the mixing should be done in the same way (and in the same order) as in the last step. It isn't "truly" random. But the way it is done is kinda arbitrary.
  • Continue doing this until you run out of ingredients. When you're done, your final "hash" will be in pot #3. It'll probably taste disgusting, but hey, all you wanted to do was combine a bunch of ingredients together in a unique way! No one said it had to make sense.
StringHash basically does this^ with the characters in your string. The key things to note are: (1) the integer value doesn't have to make sense to us--it just has to be reasonably unique, and (2) hashing the same string should give the same result each time. Not all hash functions are the same--and Blizzard's one definitely has cases where two strings will give the same value, but it is good enough for most purposes.

Here is how you'd use it:
  • Set TempString = "Rect1"
  • Custom script: set udg_TempInteger = StringHash(udg_TempString)
  • Hashtable - Save Handle Of Lane 1 Portal <gen> as 1 of TempInteger in Hashtable1
 
Last edited:
Status
Not open for further replies.
Top