codice:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Pagina senza titolo</title>
<script language="javascript" type="text/javascript">
// <!CDATA[
function EsadecimaleBinario(n)
{
var d = base_to_dec(16, n);
s = dec_to_base(d, 2);
return s;
}
function BinarioEsadecimale(n)
{
var d = base_to_dec(2, n);
s = dec_to_base(d, 16);
return s;
}
function dec_to_base(numero, base)
{
//if(!isnumber(numero)) return undefined;
if(isNaN(numero)) return undefined;
var quoziente, resto;
var simboli = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'];
var s = "";
numero = parseInt(numero);
base = parseInt(base);
do
{
quoziente = parseInt(numero / base);
resto = parseInt(numero % base);
numero = quoziente;
s = simboli[resto] + s;
} while (quoziente > 0);
return s;
}
function base_to_dec(base, s)
{
base = parseInt(base);
var c = s.length;
var i = 0;
var j = 0;
var t;
var n = 0;
var k = 0;
var bk = 1;
for (i = 0; i < c; i++)
{
j = (c - 1) - i;
t = s.charCodeAt(j);
if(t >= 48 && t <= 57) t -= 48;
else if(t >= 65 && t <= 70) t -= 55;
else if(t >= 97 && t <= 102) t -= 87;
else t = null;
if (t >= base) t = null;
bk = 1;
for(k = 1; k <= i; k++) bk *= base;
n += parseInt(t) * parseInt(bk);
}
return n;
}
// ]]>
</script>
</head>
<body>
<input id="Button1" type="button" value="Esadecimale -> binario" onclick="alert(EsadecimaleBinario('19AF'));" />
<input id="Button2" type="button" value="binario -> esadecimale" onclick="alert(BinarioEsadecimale('1100110101111'));" />
</body>
</html>