Snippet Custom Base Converter

The Undaddy

Creating with the power of rage
Reaction score
55
Custom Base Converter
v 1.00
"The simplest and most easily modifiable converter out there"

What it does?
It converts integers into some other custom base,transforming them into strings and vice versa.One (1) constant function to declare the characters list,2 functions to make the snippet work.

How to implement?
Copy the functions in the map header and (optionally) the Essentials category.

How to use?
For JASS users
function Decimal2Custom takes integer Integer returns string - this function converts the input integer into a string,using the characters list. [map header]
function Custom2Decimal takes string String returns integer - this function takes the entered string and converts it back into an integer.

GUI usage
a) Converting integers into a string
Create a new variable for the integer and the string: StringVariable and IntegerVariable
Create a new action: Custom Script
Type in "set udg_StringVariable = Decimal2Custom(udg_IntegerVariable)
Now StringVariable is set to the converted string.

b) Converting the string back into an integer
Create a new variable for the integer and the string: StringVariable and IntegerVariable
Create a new action: Custom Script
Type in "set udg_IntegerVariable = Custom2Decimal(udg_StringVariable)
Now IntegerVariable is set to the converted integer.

Features?
Features all integers between -2147483646 and 2147483646.Sadly,it doesn't feature the '-' character.(Seriously,don't put '-' in the characters list).

Where's the code?
It's right here,lol :cool::
JASS:
//   Customization/Constant functions
//=====================================================================================================================================================================================

constant function Characters takes nothing returns string
    return "0ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz123456789"  //Add here all characters that will be used by your number system.DON'T add any character more tha once!
endfunction

//Note to user: If this is going to be used by players trough an input command, try not to add characters that people don't know how to find or will be annoyed to hell before they do.

//Conversion functions
//=====================================================================================================================================================================================

function Decimal2Custom takes integer Integer returns string
    local string characters = Characters()
    local integer Slots = 1
    local integer Power
    local integer Divisor
    local string array SaveChar
    local string ReturnedChar = null
    local integer SystemLength = StringLength(characters)
    local boolean negative = false
    local integer A = 1
    if Integer < 0 then
        set Integer = -Integer
        set negative = true
    endif
    if Integer > 2147483646 then
        return null
    endif
    loop
        exitwhen A > 5
        if Integer >= R2I(Pow(I2R(SystemLength), I2R(A))) then
            set Slots =  Slots + 1 
        else
        endif
        set A = A + 1
    endloop
    set Power = Slots - 1
    set A = 1
    loop
        exitwhen A > Slots
        set Divisor = R2I(Pow(I2R(SystemLength), I2R(Power)))
        set SaveChar[A] = I2S(( Integer / Divisor ))
        set Integer = ModuloInteger(Integer,Divisor)
        set Power = Power - 1
        set A = A + 1
    endloop
    set A = 1
    loop
        exitwhen A > Slots
        set SaveChar[A] = SubString(characters, S2I(SaveChar[A]), S2I(SaveChar[A]) + 1 )
        set ReturnedChar = ReturnedChar + SaveChar[A]
        set A = A + 1
    endloop
    set A = 1
    loop
        exitwhen A > Slots
        set SaveChar[A] = null
        set A = A + 1
    endloop
    if negative == true then
        set ReturnedChar = ("-" + ReturnedChar)
    endif
    return ReturnedChar
endfunction

//=====================================================================================================================================================================================

function Custom2Decimal takes string String returns integer
    local string characters = Characters()
    local integer Slots = StringLength(String)
    local integer Power = Slots - 1
    local integer array SaveInt
    local integer ReturnedInt = 0
    local boolean Skip = false
    local boolean ExistingNumber = false
    local boolean negative = false
    local integer SystemLength = StringLength(characters)
    local integer A = 1
    local integer B = 1
    if SubString(String,0,1) == "-" then
        set negative = true
        set String = SubString(String,1,Slots)
        set Slots = Slots - 1
        set Power = Power - 1
    endif
    loop
        exitwhen A > Slots
        set ExistingNumber = false
        set B = 1
        loop
            exitwhen B > SystemLength
            if SubString(String,A - 1, A) == SubString(characters, B - 1, B) then
                set SaveInt[A] = (B - 1) * R2I(Pow(I2R(SystemLength), I2R(Power)))
                set ExistingNumber = true
            else
                if GetBooleanAnd(B == SystemLength,ExistingNumber == false) then
                    call DisplayTextToForce( GetPlayersAll(), "|c00ff0000ERROR: Non-existing character in position|r " + ( I2S(A) + " |c00ff0000of the converted string!|r" ))
                    set Skip = true
                else
                endif
            endif
            set B = B + 1
        endloop
        set Power = Power - 1
        set A = A + 1
    endloop
   
    if Skip == true then
        return 0
    endif
    set A = 1
    loop
        exitwhen A > Slots
        set ReturnedInt = ReturnedInt + SaveInt[A]
        set A = A + 1
    endloop
    if negative == true then
        set ReturnedInt = -ReturnedInt
    endif
    return ReturnedInt
endfunction


FAQ

Q:What's a base?
A:No idea how to explain this one.

Q:So,I can put any characters in there?
A:Uhuh,except the '-'.

Q:Even † , ‡ , and ™ ?
A:Uh,I don't think so.


Anything else I forgot to mention?Still curious?Speak your mind.Feedback always welcome.

Update:Added a GUI version.

Usage:

Decimal to Custom:

Set TempInt = some integer
Run Convertor 10xx
Use ReturnedChar for the new string.

Custom to Decimal:

Set TempChar = some char
Run Convertor xx10
Use ReturnedInt for the converted integer.
 

Attachments

  • Custom Base Converter v1.00.w3x
    20.5 KB · Views: 243
  • Custom Base Converter v1.00 GUI.w3x
    20.9 KB · Views: 254

Rheias

New Helper (I got over 2000 posts)
Reaction score
232
It would be useful mostly for save & load. It is basically the core of those systems.
 

Rheias

New Helper (I got over 2000 posts)
Reaction score
232
> Q:Even † , ‡ , and ™ ?
A:Uh,I don't think so.

Why not? I see no reason why those characters couldn't be used.

>what's a snippet?

Daxtreme said:
First of all, the difference between a snippet and a system: usually, a snippet is a small trigger, or a few lines that can be inserted within a trigger, and that, alone, can't do much. You insert them in a trigger, such as a function call or a short trigger object (GUI), and you call them/add an event to them if needed.

By the way, I did something similiar a while ago.
 

The Undaddy

Creating with the power of rage
Reaction score
55
> Q:Even † , ‡ , and ™ ?
A:Uh,I don't think so.

Why not? I see no reason why those characters couldn't be used.
I tried with † and ™.When you type ™,it (war3) thinks its more than 1 character,because it states that there is a non-existing character at position 2 and 3 of the decoded text,which is 1 char length.
 

gref

New Member
Reaction score
33
Probably converts it to unicode or something similarly foolish.

If I ever have a need to converting something to a base in Wc3...I'll look you up and rep you.

When it converts to Hexadecimal, are the values correct as is, ie, does 0=0, 1=1 ... E=14, F=15?
Because if it could convert to Hexadecimal without modification, then people could probably use this as a runtime colour converter.
 

The Undaddy

Creating with the power of rage
Reaction score
55
When it converts to Hexadecimal, are the values correct as is, ie, does 0=0, 1=1 ... E=14, F=15?
Because if it could convert to Hexadecimal without modification, then people could probably use this as a runtime colour converter.

Converts to the new base.So if the new base is 0123456789ABCDEF (hexadecimal,felt like putting it in brackets :p),the answer is yes.
 
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