I have a method that converts an int to a base60 string (using 0-9, a-z, and A-Z chars), but can’t work out how to convert it back again. Here is my method for converting base10 to base60:
public static function toBase60(value:Number):String
{
var targetBase:uint = 60;
value = value.toString().split('.')[0];
var digits:Array = new Array();
while (value > 0)
{
digits.push(baseChars[value % targetBase]);
value = Math.floor(value / targetBase);
}
var myResult:String = digits.reverse().join('');
return myResult;
}
Works well. But how do I get the base60 string back into a base10 int? I happen to be using ActionScript 3, but really, examples in any programming language, generic explanations or sudo code would be great.
One way of doing this could be:
Looks like you have an array that maps numbers (digits) to characters, so you could build a reverse map upfront and make the lookup easier. With some code like this:
Edit
Alternative implementation, based on algorithm posted by Tom Sirgedas. This avoids calling Math.pow, though I doubt you’ll note much difference in practice (performance-wise):