javascript實現任意進位制間的轉換
<body>
<input type="text" name="startR" placeholder="初始進位制">
<input type="text" name="endR" placeholder="目標進位制">
<input type="text" name="startN" placeholder="初始值">
<br>
<button id="getBtn">開始轉換</button>
轉換的進位制數:<input type="text" name="endN">
</body>
<script>
var btn=document.querySelector('#getBtn');
btn.onclick=function startToEnd() {
var startR=document.querySelector('[name=startR]');
var startRV=startR.value;
var endR=document.querySelector('[name=endR]');
var endRV=endR.value;
var startN=document.querySelector('[name=startN]');
var startNV=startN.value;
// 轉成十進位制
var m=parseInt(startNV,startRV);
console.log('startRV:'+startRV);
console.log('endRV:'+endRV);
console.log('startNV:'+startNV);
console.log('m:'+m);
// 十進位制轉成其他進位制
m=m.toString(endRV);
console.log(m);
var endN=document.querySelector('[name=endN]');
endN.value=m;
}
</script>
</html>