• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

I need convert integer to string and vice versa...

Status
Not open for further replies.
Level 24
Joined
Aug 1, 2013
Messages
4,658
You might just use a hashtable, then store the string in the hashtable with the hash key of the string as key.
Then you can revert the hashing by looking in the hashtable for its entry.
However, the string to int has to be called first in every single case.

4 characters might be enough for this case.
You might even increase it by using a lower base... yea, only to 5 or 6 characters.
 
Level 23
Joined
Feb 6, 2014
Messages
2,466
Base 256 is pretty useless as a generic approach, you can't pass 4 characters.

Also, didn't Blizzard.j already have those functions?
Well no matter what base you use, you're always bound to a collision (Pigeonhole principle) because from what I understand from OP's post, any string length should work. Better start reading how to make a proper hashing algorithm then.

You might just use a hashtable, then store the string in the hashtable with the hash key of the string as key.
Then you can revert the hashing by looking in the hashtable for its entry.
However, the string to int has to be called first in every single case.

4 characters might be enough for this case.
You might even increase it by using a lower base... yea, only to 5 or 6 characters.
StringHash are case-insensitive though. But if that's ok to the OP, this is probably the best way.
 
Level 17
Joined
Apr 27, 2008
Messages
2,455
Sure, they won't have exactly the same internal string table if they don't have the same language of wc3 and/or there are strings defined in a local block.
Any clue about other cases ?
But i still don't really know what Trigger.edge want to do.

Talking about StringHash, wasn't it there that someone showed how the function does work, in C or whatever else ?
 
Level 6
Joined
Jul 30, 2013
Messages
282
if you don't mind the overhead.. i use this snippet for hashes that MUST be unique. (was constructed as a byproduct of a discussion on using objects as hashtable keys and that same collision issue..)

i mostly use it for stuff like player names(cant control them thus eventually some will collide.... i got a function that does name to pid lookup) also i have a command system in the map and i use a hashtable to look up the command handlers so those names better not collide..

library StringHashEx requires Table

globals
private constant integer REHASH = 1222483
private key tbKey
private Table t = tbKey //allowed due to the way Table works
endglobals

function StringHashEx takes string key returns integer
local integer sh = StringHash(key)
loop
if not t.string.has(sh) then
set t.string[sh] = key
exitwhen true
endif
exitwhen t.string[sh] == key
set sh = sh + REHASH
endloop
return sh
endfunction

endlibrary
 
Status
Not open for further replies.
Top