TriggerHappy
...
- Reaction score
- 341
Efficient Base Conversions
Version 0.1
Version 0.1
What?
I'm not going to get into the specifics of what bases are and how this snippet converts between them but I will tell you what this is used for. This basically lets you convert integers into strings. Sounds useless right? Wrong.
It can convert big integers into small strings, depending on how big the alphabet is. This is the method most people use on save and load systems. I've used this method on my own save and load system as well, which can be found here.
It can convert big integers into small strings, depending on how big the alphabet is. This is the method most people use on save and load systems. I've used this method on my own save and load system as well, which can be found here.
Usage
Here's a little example which will convert a number into binary (and back).
JASS:
scope Binary initializer onInit
function onInit takes nothing returns nothing
local integer i = 59
local string s = BaseConvertInt(i, "01")
call BJDebugMsg(s) // Converts 59 to binary (111011)
call BJDebugMsg(I2S(BaseConvertStr(s, "01"))) // Converts 111011 to decimal (59)
endfunction
endscope
Source
JASS:
//============================================================================+
// ___________ _____ +-----------------------------+ |
// | ___| ___ \/ __ \ | | |
// | |__ | |_/ /| / \/ | Efficient Base Conversions | |
// | __|| ___ \| | | TriggerHappy187 | |
// | |___| |_/ /| \__/\ | ^^^^^^^^^^^^^^^ | |
// \____/\____/ \____/ | | |
// +-----------------------------+ |
// |
// What is EBC? |
// ^^^^^^^^^^^ |
// It can convert big integers into small strings (and back) |
// depending on how big the alphabet is. |
// |
// Why is it efficient? |
// ^^^^^^^^^^^^^^^^^^^ |
// For several reasons. First of all for converting to an integer |
// if the integer is less than the base, it will just directly return |
// value without having to do all of the math. |
// |
// Second of all, once a value is used it is stored in a hashtable. |
// So if you ever use those values again, it will load straight |
// from the hashtable. |
// |
//============================================================================+
library BaseConversions
globals
private hashtable HASH=InitHashtable()
endglobals
private function GetStrPosition takes string s, string alphabet returns integer
local integer i = 0
loop
exitwhen i > StringLength(alphabet)
if SubString(alphabet, i, i + 1) == s then
return i
endif
set i = i + 1
endloop
return 0
endfunction
function BaseConvertStr takes string str, string alphabet returns integer
local integer pos = 0
local integer i = 0
local integer base = StringLength(alphabet)
local integer len = StringLength(str)
local integer hash = StringHash(str)
if len<base then
return GetStrPosition(str, alphabet)
endif
loop
exitwhen i == StringLength(str)-1
set pos = pos * base + base * GetStrPosition(SubString(str, i, i + 1), alphabet)
set i = i + 1
endloop
return pos+GetStrPosition(SubString(str, i, i + 1), alphabet)
endfunction
function BaseConvertInt takes integer int, string alphabet returns string
local integer base = StringLength(alphabet)
local integer div = 0
local integer orig = int
local string ret = ""
if int < base then
return SubString(alphabet, int, int + 1)
elseif HaveSavedString(HASH, int, base) then
return LoadStr(HASH, int, base)
endif
loop
exitwhen int <= 0
set div = int - (int / base) * base
set ret = SubString(alphabet, div, div + 1) + ret
set int = int/base
endloop
call SaveStr(HASH, orig, base, ret)
return ret
endfunction
endlibrary