Snippet Blizzards ID2S function

Romek

Super Moderator
Reaction score
964
I was just looking around in the MPQ, and found a text document (Which is actually a single trigger, with an Init_Trig function. Although most of the functions are completely useless) loaded with Debug functions by Blizzard.
I tried calling one and it didn't work, so instead, I copied it into the editor, and it worked perfectly.

I found an ID2S function which is much shorter than any current one I've seen (I searched). It works (Tested)

JASS:
function DebugIdInteger2IdString takes integer value returns string
    local string charMap = ".................................!.#$%&'()*+,-./0123456789:;<=>.@ABCDEFGHIJKLMNOPQRSTUVWXYZ[.]^_`abcdefghijklmnopqrstuvwxyz{|}~................................................................................................................................."
    local string result = ""
    local integer remainingValue = value
    local integer charValue
    local integer byteno

    set byteno = 0
    loop
        set charValue = ModuloInteger(remainingValue, 256)
        set remainingValue = remainingValue / 256
        set result = SubString(charMap, charValue, charValue + 1) + result

        set byteno = byteno + 1
        exitwhen byteno == 4
    endloop
    return result
endfunction


Credits to Blizzard :)
This could be very useful :D
 

Strilanc

Veteran Scripter
Reaction score
42
It can be made simpler. This one won't always give 4 characters, but "A" is just as useful as "...A", and no ID used in the game should give less than 4 characters.

JASS:
function I2Char takes integer i returns string
  return SubString(".................................!.#$%&'()*+,-./0123456789:;<=>.@ABCDEFGHIJKLMNOPQRSTUVWXYZ[.]^_`abcdefghijklmnopqrstuvwxyz{|}~.................................................................................................................................", i, i+1)
endfunction
function IdString takes integer v returns string
    if v == 0 then
        return ""
    endif
    return IdString(v/256) + I2Char(ModuloInteger(v, 256))
endfunction
 

Romek

Super Moderator
Reaction score
964
It actually works fine and never gives more than 4 characters.

It could be optimized though. I'm not denying that :)
 

Strilanc

Veteran Scripter
Reaction score
42
It actually works fine and never gives more than 4 characters.

It could be optimized though. I'm not denying that :)

... Which one are you talking about? Neither of them can give more than 4 characters because an integer is 32 bits.
 

Romek

Super Moderator
Reaction score
964
... Which one are you talking about? Neither of them can give more than 4 characters because an integer is 32 bits.

It can be made simpler. This one won't always give 4 characters, but "A" is just as useful as "...A",

I didn't really explain what I meant well. The current one always returns 4 characters. No matter what :)
 

Romek

Super Moderator
Reaction score
964
Question:

what does this actually do?? (sorry, newbie)

ID's are integers. With letters.
If you try to use this integer, it'll become a very long number.
For example:
JASS:
call BJDebugMsg(I2S('A000'))
Will display a long integer.
But, If you use this function, it'll display the correct code.
So:
JASS:
local integer id = S2I(I2S('A000')) // What we just did
call BJDebugMsg(DebugIdInteger2IdString(id))

Will Display "A000"
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top