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.
  • WildTurkey WildTurkey:
    is there a stephen green in the house?
    +1
  • The Helper The Helper:
    What is up WildTurkey?
  • The Helper The Helper:
    Looks like Google fixed whatever mistake that made the recipes on the site go crazy and we are no longer trending towards a recipe site lol - I don't care though because it motivated me to spend alot of time on the site improving it and at least now the content people are looking at is not stupid and embarrassing like it was when I first got back into this like 5 years ago.
  • The Helper The Helper:
    Plus - I have a pretty bad ass recipe collection now! That section of the site is 10 thousand times better than it was before
  • The Helper The Helper:
    We now have a web designer at my job. A legit talented professional! I am going to get him to redesign the site theme. It is time.
  • Varine Varine:
    I got one more day of community service and then I'm free from this nonsense! I polished a cop car today for a funeral or something I guess
  • Varine Varine:
    They also were digging threw old shit at the sheriff's office and I tried to get them to give me the old electronic stuff, but they said no. They can't give it to people because they might use it to impersonate a cop or break into their network or some shit? idk but it was a shame to see them take a whole bunch of radios and shit to get shredded and landfilled
  • The Helper The Helper:
    whatever at least you are free
  • 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 Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top