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"
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 :
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.