Help with number conversion

s3rius

Linux is only free if your time is worthless.
Reaction score
130
Hello,
I've found the following function to convert the base of numbers (e.g. decimal -> hexadecimal)
But it seems to have a malfunction, since the conversion decimal to base 62 doesn't give me the correct value.
I've also tried to convert to base 36 and that seems to work without problems.
My brain pretty much freezes when it's coming to math so I hope one of you could help me by pointing out what's wrong with the code:
(e.g.: BaseConversion( 120, 10, 32) should convert 120 from base 10 to 32)
JASS:
function BaseConversion takes string input, integer inputBase, integer outputBase returns string
    local string charMap = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@#"
    local string s
    local string result = ""
    local integer val = 0
    local integer i
    local integer p = 0
    local integer pow = 1
    local string sign = ""
    if ( inputBase < 2 or inputBase > StringLength(charMap) or outputBase < 2 or outputBase > StringLength(charMap) ) then
        // Bases are invalid or out of bounds
        return "Invalid bases given"
    endif
    if ( SubString(input, 0, 1) == "-" ) then
        set sign = "-"
        set input = SubString(input, 1, StringLength(input))
    endif
    set i = StringLength(input)
    // Get the integer value of input
    set input = StringCase(input, false)
    loop
        exitwhen i <= 0
        set s = SubString(input, i-1, i)
        set p = 0
        loop
            if ( p >= inputBase ) then
                // Input cannot match base
                return "Input does not match base!"
            endif
            if ( s == SubString(charMap, p, p+1) ) then
                set val = val + pow*p
                set pow = pow * inputBase
                exitwhen true
            endif
            set p = p + 1
        endloop
        set i = i - 1
    endloop
    loop
        set p = ModuloInteger(val, outputBase)
        set result = SubString(charMap, p, p+1) + result
        set val = val / outputBase
        exitwhen val <= 0
    endloop
    return sign + result
endfunction


EDIT: Oh my god, don't mind that post. Me baka overlooked that the whole string got converted into lower case, thus upper case letters didn't work anymore >.<
 

MindWorX

New Member
Reaction score
8
Looks like an interesting function. But i'm worried about all the substrings, since everytime you create a new string like that, it leaks, and can't be cleaned.
 

s3rius

Linux is only free if your time is worthless.
Reaction score
130
Afaik, string leaks are not very major and won't do much harm at all. Also if you keep leaks minimal throughout your game, a few leaks won't do anything.
 

Trollvottel

never aging title
Reaction score
262
never heard of leaking strings..... reals integers booleans and strings dont leak afaik
 

MindWorX

New Member
Reaction score
8
Everytime you create a unique string, a handle is created, and since there's no way to destroy strings, you can't clean that handle. It's not a major leak when done in a normal game, but the way you abuse substrings will cause it to create a lot of string handles. But, looking at your function, the place where it abuses SubString the most is where it only creates strings of 1 char long, which will stop leaking once it's made a string with every single sign/letter/digit.
 

SFilip

Gone but not forgotten
Reaction score
634
> Strings don't increment the reference count
Not the handle reference count, but they have one of their own which they increase.
JASS:
function StoI takes string s returns integer
    return s
    return 0
endfunction

function proof takes nothing returns nothing
    call BJDebugMsg(I2S(StoI(&quot;this is a string&quot;)))
    call BJDebugMsg(I2S(StoI(&quot;this is a string&quot;)))
    call BJDebugMsg(I2S(StoI(&quot;this is another string&quot;)))
endfunction

Displays two same numbers and one that is higher.

> and only take up a few bytes. Not that major.
One can say the same about any leak.
Having too many strings is probably not a good idea regardless of the memory usage.
 
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