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)
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 >.<
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 >.<