Snippet EBC - Efficient Base Conversions

Reaction score
341
Efficient Base Conversions
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.​

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
 

Nestharus

o-o
Reaction score
84
It makes it more modular... big features within systems should be open for other systems to use.

Currently, there is no very good base conversion utility, so this will be a first.

Recommendations-
Use reals instead of integers so you can get huge numbers.

You should convert from any base to any base.
str base1, str num1, str base2, str num2

str real str real
str str str str
str real str str
str str str real

Special functions for common bases. All of these should be convert to and convert from =).
Base 2
Base 8
Base 10
Base 16

I can't stress using reals enough ><, lol.

Why reals?
So it can handle very large numbers.
 

Nestharus

o-o
Reaction score
84
Actually, we already had this convo in another thread, lol.

Reals can handle really big numbers or really small numbers. It goes to 32 bit if you have a combo :eek:.
 
Reaction score
341
Other than save and load it can provide a StringHash simulation function for mac users who can't use the new natives. Don't know about linux. Not to mention that the returned numbers will be alot smaller than the ones from StringHash.

JASS:
library GetStringId requires BaseConversions

    globals
        private constant string BLIZZARD_CHARMAP = &quot;!.#$%&amp;&#039;()*+,-./0123456789:;&lt;=&gt;.@ABCDEFGHIJKLMNOPQRSTUVWXYZ[.]^_`abcdefghijklmnopqrstuvwxyz{|}~ &quot;
    endglobals
    
    function GetStringId takes string s returns integer
        return BaseConvertStr(s, BLIZZARD_CHARMAP)
    endfunction
    
endlibrary
 
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