1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
<head>
<title>Conversion Décimal / Hexa</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name="Author" content="Bovino - www.developpez.com" />
<script type="text/javascript">
<!--
function toHexa(){
var regDec = /^\d+$/;
var initial = document.getElementById('valeur').value;
if(regDec.test(initial)){
var converti = parseInt(initial,10).toString(16).toUpperCase();
document.getElementById('resultat').innerHTML = 'Décimal : '+initial+' => Hexadécimal : '+converti;
}
else{
alert('La valeur entrée n\'est pas décimale !');
}
}
function toDec(){
var regHexa = /^[a-f\d]+$/i;
var initial = document.getElementById('valeur').value.toUpperCase();
if(regHexa.test(initial)){
var converti = parseInt(initial,16);
document.getElementById('resultat').innerHTML = 'Hexadécimal : '+initial+' => Décimal : '+converti;
}
else{
alert('La valeur entrée n\'est pas hexadécimale !');
}
}
//-->
</script>
</head>
<body>
<div style="margin: 50px 0 0 50px">
<label for="valeur">Entrez la valeur à convertir : </label>
<input type="text" id="valeur" onfocus="this.value=''" /><br />
<input type="button" onclick="toHexa()" value="Convertir en hexadécimal" />
<input type="button" onclick="toDec()" value="Convertir en décimal" />
</div>
<div id="resultat" style="text-align:center;margin-top:20px;"></div>
</body>
</html> |
Partager