|
|
@ -1,86 +0,0 @@
|
|
|
|
<!DOCTYPE html>
|
|
|
|
|
|
|
|
<html lang="zh">
|
|
|
|
|
|
|
|
<head>
|
|
|
|
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
|
|
|
|
|
<title>进制转换器</title>
|
|
|
|
|
|
|
|
<style>
|
|
|
|
|
|
|
|
body {
|
|
|
|
|
|
|
|
font-family: Arial, sans-serif;
|
|
|
|
|
|
|
|
margin: 20px;
|
|
|
|
|
|
|
|
padding: 20px;
|
|
|
|
|
|
|
|
border: 1px solid #ccc;
|
|
|
|
|
|
|
|
border-radius: 10px;
|
|
|
|
|
|
|
|
max-width: 400px;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
label, input, button {
|
|
|
|
|
|
|
|
display: block;
|
|
|
|
|
|
|
|
margin-bottom: 10px;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#result {
|
|
|
|
|
|
|
|
font-weight: bold;
|
|
|
|
|
|
|
|
color: #333;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
.error {
|
|
|
|
|
|
|
|
color: red;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
</style>
|
|
|
|
|
|
|
|
</head>
|
|
|
|
|
|
|
|
<body>
|
|
|
|
|
|
|
|
<h2>任意进制转换器</h2>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<label for="fromBase">输入进制 (2-16):</label>
|
|
|
|
|
|
|
|
<input type="number" id="fromBase" min="2" max="16" required>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<label for="number">输入数字:</label>
|
|
|
|
|
|
|
|
<input type="text" id="number" placeholder="例如:1010" required>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<label for="toBase">转换为进制 (2-16):</label>
|
|
|
|
|
|
|
|
<input type="number" id="toBase" min="2" max="16" required>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<button id="convertButton">转换</button>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<h3>结果:</h3>
|
|
|
|
|
|
|
|
<div id="result"></div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<script>
|
|
|
|
|
|
|
|
document.getElementById('convertButton').addEventListener('click', handleConversion);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function handleConversion() {
|
|
|
|
|
|
|
|
const fromBase = parseInt(document.getElementById('fromBase').value);
|
|
|
|
|
|
|
|
const number = document.getElementById('number').value.trim();
|
|
|
|
|
|
|
|
const toBase = parseInt(document.getElementById('toBase').value);
|
|
|
|
|
|
|
|
const resultElement = document.getElementById('result');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 清除之前的结果和错误信息
|
|
|
|
|
|
|
|
resultElement.textContent = '';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 验证输入的数字是否合法
|
|
|
|
|
|
|
|
const validationMessage = validateInput(number, fromBase);
|
|
|
|
|
|
|
|
if (validationMessage) {
|
|
|
|
|
|
|
|
resultElement.textContent = validationMessage;
|
|
|
|
|
|
|
|
resultElement.className = 'error';
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 转换为十进制并转换为目标进制
|
|
|
|
|
|
|
|
const decimalValue = parseInt(number, fromBase);
|
|
|
|
|
|
|
|
const convertedValue = decimalValue.toString(toBase).toUpperCase();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 显示结果
|
|
|
|
|
|
|
|
resultElement.textContent = convertedValue;
|
|
|
|
|
|
|
|
resultElement.className = ''; // 清除错误样式
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function validateInput(numStr, base) {
|
|
|
|
|
|
|
|
const validChars = '0123456789ABCDEF'.slice(0, base);
|
|
|
|
|
|
|
|
const isValid = [...numStr].every(char => validChars.includes(char.toUpperCase()));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!isValid) {
|
|
|
|
|
|
|
|
return `输入的数字不符合${base}进制规则!`;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
</body>
|
|
|
|
|
|
|
|
</html>
|
|
|
|
|