|
|
|
|
@ -0,0 +1,94 @@
|
|
|
|
|
<!DOCTYPE html>
|
|
|
|
|
<html lang="en">
|
|
|
|
|
<head>
|
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
|
|
<title>进制转换</title>
|
|
|
|
|
<style>
|
|
|
|
|
/* 简单的CSS样式 */
|
|
|
|
|
body {
|
|
|
|
|
font-family: Arial, sans-serif;
|
|
|
|
|
margin: 20px;
|
|
|
|
|
}
|
|
|
|
|
.container {
|
|
|
|
|
max-width: 400px;
|
|
|
|
|
margin: 0 auto;
|
|
|
|
|
padding: 20px;
|
|
|
|
|
border: 1px solid #ccc;
|
|
|
|
|
border-radius: 5px;
|
|
|
|
|
}
|
|
|
|
|
label, input, select, button {
|
|
|
|
|
display: block;
|
|
|
|
|
margin-bottom: 10px;
|
|
|
|
|
}
|
|
|
|
|
input, select, button {
|
|
|
|
|
width: 100%;
|
|
|
|
|
padding: 8px;
|
|
|
|
|
box-sizing: border-box;
|
|
|
|
|
}
|
|
|
|
|
#result {
|
|
|
|
|
margin-top: 20px;
|
|
|
|
|
font-weight: bold;
|
|
|
|
|
}
|
|
|
|
|
</style>
|
|
|
|
|
</head>
|
|
|
|
|
<body>
|
|
|
|
|
<div class="container">
|
|
|
|
|
<h2>进制转换</h2>
|
|
|
|
|
<label for="inputNumber">输入数字:</label>
|
|
|
|
|
<input type="text" id="inputNumber" placeholder="请输入2-16进制数字">
|
|
|
|
|
|
|
|
|
|
<label for="inputBase">原始进制:</label>
|
|
|
|
|
<select id="inputBase">
|
|
|
|
|
<!-- 使用JavaScript动态生成选项 -->
|
|
|
|
|
<script>
|
|
|
|
|
for (let i = 2; i <= 16; i++) {
|
|
|
|
|
document.write('<option value="' + i + '">' + i + '</option>');
|
|
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
</select>
|
|
|
|
|
|
|
|
|
|
<label for="outputBase">目标进制:</label>
|
|
|
|
|
<select id="outputBase">
|
|
|
|
|
<!-- 同上,使用JavaScript动态生成选项 -->
|
|
|
|
|
<script>
|
|
|
|
|
for (let i = 2; i <= 16; i++) {
|
|
|
|
|
document.write('<option value="' + i + '">' + i + '</option>');
|
|
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
</select>
|
|
|
|
|
|
|
|
|
|
<button onclick="convertBase()">转换</button>
|
|
|
|
|
|
|
|
|
|
<div id="result"></div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<script>
|
|
|
|
|
function convertBase() {
|
|
|
|
|
const inputNumber = document.getElementById('inputNumber').value;
|
|
|
|
|
const inputBase = parseInt(document.getElementById('inputBase').value, 10);
|
|
|
|
|
const outputBase = parseInt(document.getElementById('outputBase').value, 10);
|
|
|
|
|
|
|
|
|
|
let decimalValue = parseInt(inputNumber, inputBase);
|
|
|
|
|
if (isNaN(decimalValue)) {
|
|
|
|
|
alert('输入的数字或进制无效!');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let result = '';
|
|
|
|
|
let digits = '0123456789ABCDEF';
|
|
|
|
|
|
|
|
|
|
while (decimalValue > 0) {
|
|
|
|
|
result = digits[decimalValue % outputBase] + result;
|
|
|
|
|
decimalValue = Math.floor(decimalValue / outputBase);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (result === '') {
|
|
|
|
|
result = '0';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
document.getElementById('result').innerText = '转换结果:' + result;
|
|
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
</body>
|
|
|
|
|
</html>
|