开发者

convert from base 60 to base 10

开发者 https://www.devze.com 2023-01-09 02:31 出处:网络
I 开发者_如何学Pythonhave 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

I 开发者_如何学Pythonhave 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.


total = 0;
for each digit (front to back)
  total = total * 60 + digit


One way of doing this could be:

    public static function fromBase60(value:String):Number {
        var result:Number = 0;
        var targetBase:uint = 60;
        var digitValue:int = 0;
        for(var i:int = 0, j:int = value.length - 1; j >= 0; i++,j--) {
            digitValue = reverseMap[value.charAt(j)];
            result += Math.pow(targetBase,i) * digitValue; 
        }
        return result;
    }

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:

    // add this code to your class
    private static var reverseMap:Object = {};

    private static function buildReverseMap():void {
        var len:int = baseChars.length;
        for(var i:int = 0; i < len; i++) {
            reverseMap[baseChars[i]] = i;
        }
    }

    // initialize the reverse map
    {
        buildReverseMap();
    }

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

    public static function fromBase60(value:String):Number {
        var result:Number = 0;
        var targetBase:uint = 60;
        var digitValue:int = 0;
        var len:int = value.length;
        for(var i:int = 0; i < len; i++) {
            digitValue = reverseMap[value.charAt(i)];
            result = result * targetBase + digitValue; 
        }
        return result;
    }


Assuming you have a function int digit60to10(char digit) that converts [0-9a-zA-Z] into the equivalent decimal value for a single digit, you can do this:

int decimalValue = 0;
foreach digit in digits (most to least significant):
  decimalValue *= 60;
  decimalValue += digit60to10(digit);


This could give you some problems. But that is not Base 60, it's base 62

Edit Since the above is not a viewed as a valid answer, here is how I converted base 62 <=> 10 in PHP, though there are many methods to do so. http://ken-soft.com/?p=544

I also, explained why I posted that as an answer in the below comment (though I agree it was way too brief) :) sorry.
Edit why is this getting voted down? What I said is true!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号