Bantas Anomalie floodiforme
Inscrit le: 21 Aoû 2007 Messages: 1524 Sujets: 37
Médailles: 1 (En savoir plus...)
|
Posté le: 17/11/07 19:53 Sujet du message: BaseConversion |
|
|
Nom de la fonction : BaseConversion
Créateur : KaTTana
Fonctions requises : aucune à prori
Code : Jass: | function BaseConversion takes string input, integer inputBase, integer outputBase returns string
local string charMap = "0123456789abcdefghijklmnopqrstuvwxyz"
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 |
Utilisation : Bah ça sert à convertir des entiers d'une base à l'autre (par exemple si vous voulez vos couleurs en hexadécimal sans avoir à chercher).
Copyright : Euh j'en sais rien, j'ai trouvé ça là (grâce à Troll-Brain) : http://wc3jass.com/viewtopic.php?t=178
Remarques : Citation: | Converts a number of one base to another base. It's fairly optimized although there may be faster methods.
Parameters:
string input - This is the number which you want converted to another base.
integer inputBase - This is the base of input
integer outputBase - This is the base you want to convert it to.
The bases cannot be longer than the length of charMap, but you can add more characters to it if you want.
If an error occoured, it will return a string telling what went wrong.
Empty strings and null strings return 0. |
Convertit un nombre d'une base à une autre. C'est bien optimisé même si il peut y avoir des méthodes plus rapides.
Arguments :
string input - Nombre à convertir dans une autre base.
string inputBase - Base de input.
string outputBase - Base dans laquelle le nombre sera converti.
Les bases ne peuvent pas être plus longue que charMap, mais vous pouvez ajouter plus de lettres si vous le voulez.
Si une erreur se produit, la fonction renverra une chaîne de texte disant ce qui s'est (mal) passé.
Les chaînes de texte vides ou inexistantes renverront 0. _________________
|
|