Warcraft III resources & community, 2003–2006 · archived

Integer/Alphanumeric Converter

not rated
Submitted by Die_BackeConversion0 downloads
It was made for savesystems, and makes an integer need less digets. If you just use I2S() it is easy to see how the system works, and my converter often can save some digets.

Int2Alphanumeric(int, charmap) (string)
Returns the passed integer as a string. If the charmap is longer, the returned string will be smaller. Don't use any letter or number twice in the charmap. The returned string often has less digets than the passed integer.

Alphanumeric2Int(str, charmap) (integer)
for what you need to make a number needing less digets, if you can't get the number from it again? The string returned by the function above, must be passed to this function with the same charmap, to get the number passed to the function above.

If the charmap is "0123456789ABCDEF" the converter returns the passed number in hex. If the charmap is "01" it returns the number in binary.

I'll try to explain how it works. usually, we count from 0 to 9, start from 0 again, while the next digit increases by one, as the first one did before. when that digit reaches the number after 9, it also restarts from 0, while the next digit increases by one. this script counts in the same way, but it counts in ONE digit, until the length of the charmap is too small.
To calculate the biggest number, that can be saved with x digits, and a charmap with an length of y, we have y^x-1. if the charmap has e.g. 64 digits, that can be a very big number, with only a few digits ;)

IMPORTANT!! NEEDS MY IndexOfSubstring FUNCTION!

Source

function Int2Alphanumeric takes integer int, string charmap returns string
    local string alphanumeric = ""
    local integer index = 0
    if ((int < 0) or (int > 0x7FFFFFFF)) then
        return "|cffff0000Invalid parameter passed to function Int2Alphanumeric()!|r"
    endif
    loop
        set index = ModuloInteger(int, StringLength(charmap)) + 1
        set int = int / StringLength(charmap)
        set alphanumeric = SubStringBJ(charmap, index, index) + alphanumeric
        exitwhen (int == 0)
    endloop
    return alphanumeric
endfunction
function Alphanumeric2Int takes string str, string charmap returns integer
    local integer int = 0
    local integer index = 1
    loop
        set int = int + ( ( IndexOfSubstring( charmap, SubString(str, StringLength(str) - 1, StringLength(str)) ) ) * index )
        exitwhen StringLength(str) == 1
        set str = SubStringBJ( str, 1, StringLength(str) - 1 )
        set index = index * StringLength(charmap)
    endloop
    return int
endfunction

Comments

0

No comments.