数制间的相互转换

function mulBase(n, base) {
    var base16 = [{10: 'A'}, {11: 'B'}, {12: 'C'}, {13: 'D'}, {14: 'E'}, {15: 'F'}];
    var arr = [];
    var res = '';

    while (n > 0) {
        arr.push(n % base);
        n = Math.floor(n / base);
    }

    if (base == 16) {
        while (!!arr.length) {
            var temp = arr.pop();
            for (var i = 0, len = base16.length; i < len; i++) {
                if (temp in base16[i]) {
                    temp = base16[i][temp];
                    break;
                }
            }
            res += temp;
        }
    }
    else {
        while (!!arr.length) {
            res += arr.pop();
        }
    }

    return res;
}

 

posted @ 2016-07-08 16:05  koala_熊  Views(116)  Comments(0Edit  收藏  举报