89 lines
2.7 KiB
HTML
89 lines
2.7 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="it">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Convertitore Base 10 a Base 2, 8, 16</title>
|
|
<style>
|
|
body {
|
|
font-family: Arial, sans-serif;
|
|
margin: 30px;
|
|
}
|
|
input, button {
|
|
padding: 10px;
|
|
margin: 10px 0;
|
|
}
|
|
label {
|
|
font-weight: bold;
|
|
}
|
|
.result {
|
|
margin-top: 20px;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
|
|
<h1>Convertitore Base 10 a Base 2, 8, 16</h1>
|
|
|
|
<label for="num">Inserisci un numero in base 10:</label>
|
|
<input type="number" id="num" required>
|
|
|
|
<button onclick="convert()">Converti</button>
|
|
|
|
<div class="result">
|
|
<p><strong>Base 2:</strong> <span id="binaryResult"></span></p>
|
|
<p><strong>Base 8:</strong> <span id="octalResult"></span></p>
|
|
<p><strong>Base 16:</strong> <span id="hexResult"></span></p>
|
|
</div>
|
|
|
|
<script>
|
|
function convert() {
|
|
// Ottieni il numero in base 10 inserito
|
|
let num = parseInt(document.getElementById("num").value);
|
|
|
|
// Funzione per convertire in base 2
|
|
function toBinary(n) {
|
|
let binary = '';
|
|
while (n > 0) {
|
|
binary = (n % 2) + binary;
|
|
n = Math.floor(n / 2);
|
|
}
|
|
return binary === '' ? '0' : binary;
|
|
}
|
|
|
|
// Funzione per convertire in base 8
|
|
function toOctal(n) {
|
|
let octal = '';
|
|
while (n > 0) {
|
|
octal = (n % 8) + octal;
|
|
n = Math.floor(n / 8);
|
|
}
|
|
return octal === '' ? '0' : octal;
|
|
}
|
|
|
|
// Funzione per convertire in base 16
|
|
function toHex(n) {
|
|
const hexChars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'];
|
|
let hex = '';
|
|
while (n > 0) {
|
|
hex = hexChars[n % 16] + hex;
|
|
n = Math.floor(n / 16);
|
|
}
|
|
return hex === '' ? '0' : hex;
|
|
}
|
|
|
|
// Esegui le conversioni
|
|
let binaryResult = toBinary(num);
|
|
let octalResult = toOctal(num);
|
|
let hexResult = toHex(num);
|
|
|
|
// Mostra i risultati
|
|
document.getElementById("binaryResult").textContent = binaryResult;
|
|
document.getElementById("octalResult").textContent = octalResult;
|
|
document.getElementById("hexResult").textContent = hexResult;
|
|
}
|
|
</script>
|
|
|
|
</body>
|
|
</html>
|