您的位置:首页 > Web前端 > JavaScript

js实现从10进制转换为2/8/16进制

2017-04-07 00:00 411 查看
//栈的全部代码
function Stack() {
var items=[];
this.push=function (element) {
items.push(element);
};
this.pop=function () {
return items.pop();
};
this.peek=function () {
return items[items.length-1];//返回栈顶元素
};
this.isEmpty=function () {
return items.length==0;
};
this.size=function () {
return items.length;
};
this.clear=function () {
items=[];
};
this.print=function () {
console.log(items.toString());
};

}
//进制转换
function baseConverter(decNumber,base) {
var remStack=new Stack(),
rem,
baseString='',
digits='0123456789ABCDEF';
while (decNumber>0){
rem=Math.floor(decNumber%base);
remStack.push(rem);
decNumber=Math.floor(decNumber/base);
}
while (!remStack.isEmpty()){
baseString+=digits[remStack.pop()];
}
return baseString;
}
console.log(baseConverter(100345,16));
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: