59 lines
1.8 KiB
HTML
59 lines
1.8 KiB
HTML
<html>
|
|
|
|
<!--
|
|
Nome: Mario
|
|
Cognome: Montanari
|
|
-->
|
|
|
|
<head>
|
|
<link rel="icon" href="itisnullobaldiniravenna.jpg" />
|
|
<link rel="stylesheet" href="styleConversioneDecimaleEsadecimale.css" />
|
|
<title>Conversione da decimale a esidecimale</title>
|
|
</head>
|
|
<body>
|
|
<h1>Numero in Base 10:</h1>
|
|
<input type="text" id="numeroDecimale" placeholder="Inserisci un numero decimale tra 0 e 255" /></br>
|
|
<button onclick="conversioneEsadecimale()">Converti in base 16</button></br></br>
|
|
<input id="numeroEsadecimale" disabled />
|
|
<script>
|
|
|
|
function conversioneEsadecimale() {
|
|
let numeroDecimale = document.getElementById("numeroDecimale").value;
|
|
let numeroEsadecimale = "";
|
|
|
|
if (numeroDecimale !== "") {
|
|
if (numeroDecimale >= 0 && numeroDecimale <= 255) {
|
|
if (numeroDecimale === 0) {
|
|
numeroEsadecimale = "0";
|
|
}
|
|
while (numeroDecimale > 0) {
|
|
let restoEsadecimale = numeroDecimale % 16;
|
|
if (restoEsadecimale === 10) {
|
|
restoEsadecimale = "A";
|
|
} else if (restoEsadecimale === 11) {
|
|
restoEsadecimale = "B";
|
|
} else if (restoEsadecimale === 12) {
|
|
restoEsadecimale = "C";
|
|
} else if (restoEsadecimale === 13) {
|
|
restoEsadecimale = "D";
|
|
} else if (restoEsadecimale === 14) {
|
|
restoEsadecimale = "E";
|
|
} else if (restoEsadecimale === 15) {
|
|
restoEsadecimale = "F";
|
|
}
|
|
numeroEsadecimale = restoEsadecimale + numeroEsadecimale;
|
|
numeroDecimale = Math.floor(Number(numeroDecimale) / 16);
|
|
}
|
|
} else {
|
|
window.alert("Numero non valido! Inserisci un numero compreso tra 0 e 255.");
|
|
}
|
|
} else if (numeroDecimale === "") {
|
|
window.alert("Inserisci un numero!");
|
|
}
|
|
|
|
document.getElementById("numeroEsadecimale").value = numeroEsadecimale;
|
|
}
|
|
|
|
</script>
|
|
</body>
|
|
</html> |