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.
  • Monovertex Monovertex:
    How are you all? :D
    +1
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though

      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