- Joined
- May 1, 2007
- Messages
- 51
The following function is a generic base converter.
It can convert base 10 (numbers) into any base you provide the tables for.
Possible uses include -save codes.
Tables do not have to be the same, for example:
Is a valid table array.
The number of defined tables specify the maximum lenght of a code, and as such the maximum number you can pass to the function.
For example:
Would allow only the creation of 1 character codes, from numbers in the range 0-25
While:
Would allow the creation of 2 character codes, from numbers in the range 0-675
To calculate the maximum valid number for any number of characters:
(num_chars^defined_tables)-1
For example, if you want to use two characters for your code, you first have to define at least table[0] and table[1]:
Then to calculate the range of numbers you can convert to a two character code: 0 to (26^2)-1 which equals: 0 to 675
Where 26 is the length of the tables.
NOTE! All tables must be the same length
The actuall functions:
Usage:
Download demo map: View attachment compressor.w3x
Questions?
It can convert base 10 (numbers) into any base you provide the tables for.
Possible uses include -save codes.
Tables do not have to be the same, for example:
JASS:
set table[0]="ABCDEQRSTUVWFGHIJKLMNOPXYZ"
set table[1]="QRSTUVWABCDEFGHIJKLMNOPXYZ"
set table[2]="FGHIJKLMNOPQRSTUVWXYZABCDE"
set table[3]="IJKLMNOPQRSTUVWXYZABCDEFGH"
The number of defined tables specify the maximum lenght of a code, and as such the maximum number you can pass to the function.
For example:
JASS:
set table[0]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
While:
JASS:
set table[0]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set table[1]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
To calculate the maximum valid number for any number of characters:
(num_chars^defined_tables)-1
For example, if you want to use two characters for your code, you first have to define at least table[0] and table[1]:
JASS:
set table[0]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set table[1]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Where 26 is the length of the tables.
NOTE! All tables must be the same length
The actuall functions:
JASS:
function FVO_CHR takes integer a, integer b returns string
local string array table
set table[0]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set table[1]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set table[2]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set table[3]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
//If you need codes longer than 4 characters fill out more tables.
if b<=0 or a<=0 then
return I2S(StringLength(table[0])-1)
else
return SubString(table[b-1], a-1, a)
endif
endfunction
function FVO_Compress takes integer v, integer l returns string
local integer code_size=S2I(FVO_CHR(-1,-1))
local integer codehead=1
local integer cdata=0
local integer vdata=v
local integer loop_a=0
local string retc=""
set loop_a = 1
loop
exitwhen loop_a > l-1
set codehead=codehead*code_size
set loop_a=loop_a+1
endloop
set loop_a = 1
loop
exitwhen loop_a > l
if vdata>=codehead then
set cdata = vdata / codehead
set vdata = vdata - cdata * codehead
else
set cdata = 0
endif
set cdata = cdata + 1
set retc = retc + FVO_CHR(cdata, loop_a)
set codehead = codehead / code_size
set loop_a = loop_a + 1
endloop
return retc
endfunction
function FVO_DeCompress takes string v returns integer
local integer code_size=S2I(FVO_CHR(-1,-1))
local integer clen = StringLength(v)
local integer codehead = 1
local integer loop_a=0
local integer loop_b=0
local integer reti=0
set loop_a = 1
loop
exitwhen loop_a > clen-1
set codehead=codehead*code_size
set loop_a=loop_a+1
endloop
set loop_a = 1
loop
exitwhen loop_a > clen
set loop_b = 1
loop
exitwhen loop_b > code_size
if FVO_CHR(loop_b, loop_a)==SubString(v, loop_a-1, loop_a) then
set reti = reti + codehead * (loop_b - 1)
endif
set loop_b = loop_b + 1
endloop
set codehead = codehead / code_size
set loop_a = loop_a + 1
endloop
return reti
endfunction
JASS:
local string test1
local integer test2
set test1 = FVO_Compress(123456, 4)
call DisplayTimedTextToForce( GetPlayersAll(), 60.00, "FVO_Compress(123456, 4) = "+test1 )
set test2 = FVO_DeCompress(test1)
call DisplayTimedTextToForce( GetPlayersAll(), 60.00, "FVO_DeCompress(\""+test1+"\") = "+I2S(test2) )
call DisplayTimedTextToForce( GetPlayersAll(), 1.00, " " )
set test1 = FVO_Compress(12345, 3)
call DisplayTimedTextToForce( GetPlayersAll(), 60.00, "FVO_Compress(12345, 3) = "+test1 )
set test2 = FVO_DeCompress(test1)
call DisplayTimedTextToForce( GetPlayersAll(), 60.00, "FVO_DeCompress(\""+test1+"\") = "+I2S(test2) )
call DisplayTimedTextToForce( GetPlayersAll(), 1.00, " " )
set test1 = FVO_Compress(123, 2)
call DisplayTimedTextToForce( GetPlayersAll(), 60.00, "FVO_Compress(123, 2) = "+test1 )
set test2 = FVO_DeCompress(test1)
call DisplayTimedTextToForce( GetPlayersAll(), 60.00, "FVO_DeCompress(\""+test1+"\") = "+I2S(test2) )
call DisplayTimedTextToForce( GetPlayersAll(), 1.00, " " )
set test1 = FVO_Compress(12, 1)
call DisplayTimedTextToForce( GetPlayersAll(), 60.00, "FVO_Compress(12, 1) = "+test1 )
set test2 = FVO_DeCompress(test1)
call DisplayTimedTextToForce( GetPlayersAll(), 60.00, "FVO_DeCompress(\""+test1+"\") = "+I2S(test2) )
Download demo map: View attachment compressor.w3x
Questions?