Javascript Base Converter

Javascript base converter is used for number conversion,base conversion.Decimal,binary,hexadecimal,octal number conversion.

Features

  • You can convert a number to decimal,binary,octal,hexadecimal value using this javascript.
  • Just copy the code and use it.
  • This Base converter is for free use.

Preview

Base Converter
Enter a number in any one of input box
Decimal: (0..9)
Binary: (0,1)
Octal: (0..7)
Hexadecimal: (0..9,A..F,a..f)
©hscripts.com

Downloads

  Javascript Code

<!-- Script by hscripts.com --><script type="text/javascript">
function dconvert()
{
var dnum=parseInt(document.getElementById("deci").value);
var dno=new Number(dnum);
if(!isNaN(dno))
{
document.getElementById("bin").value=dno.toString(2);
document.getElementById("oct").value=dno.toString(8);
document.getElementById("hdeci").value=dno.toString(16);
}
}
function bconvert(bn)
{
var len=bn.length;
var tot=0;
var i,j;
for(i=0,j=len-1;i<len;i++,j--)
{
tot=tot+parseInt(bn.charAt(i))*Math.pow(2,j);
}
var dec=new Number(tot);
document.getElementById("deci").value=dec;
document.getElementById("oct").value=dec.toString(8);
document.getElementById("hdeci").value=dec.toString(16);
}
function oconvert(oc)
{
var len=oc.length;
var tot=0;
var i,j;
for(i=0,j=len-1;i<len;i++,j--)
{
tot=tot+parseInt(oc.charAt(i))*Math.pow(8,j);
}
var dec=new Number(tot);
document.getElementById("deci").value=dec;
document.getElementById("bin").value=dec.toString(2);
document.getElementById("hdeci").value=dec.toString(16);
}
function hconvert(hd)
{
var len=hd.length;
var tot=0;
var i,j;
for(i=0,j=len-1;i<len;i++,j--)
{
ch=hd.charAt(i);
if(ch=="a" || ch=="A")
{
tot=tot+10;
}
else if(ch=="b" || ch=="B")
{
tot=tot+11;
}
else if(ch=="c" || ch=="C")
{
tot=tot+12;
}
else if(ch=="d" || ch=="D")
{
tot=tot+13;
}
else if(ch=="e" || ch=="E")
{
tot=tot+14;
}
else if(ch=="f" || ch=="F")
{
tot=tot+15;
}
else
{
tot=tot+parseInt(hd.charAt(i))*Math.pow(16,j);
}
}
var dec=new Number(tot);
document.getElementById("deci").value=dec;
document.getElementById("bin").value=dec.toString(2);
document.getElementById("oct").value=dec.toString(8);
}
function dncheck(event,no)
{
var kc;
if(window.event)
{
kc=event.keyCode;
}
else
{
kc=event.which;
}
var num=no.value;
if (kc!=8 && kc!=0)
{
if (kc<48||kc>57)
{
return false;
}
}
}
function bncheck(event,no)
{
var kc;
if(window.event)
{
kc=event.keyCode;
}
else
{
kc=event.which;
}
var num=no.value;
if (kc!=8 && kc!=0)
{
if (kc!=48 && kc!=49)
{
return false;
}
}
}
function oncheck(event,no)
{
var kc;
if(window.event)
{
kc=event.keyCode;
}
else
{
kc=event.which;
}
var num=no.value;
if (kc!=8 && kc!=0)
{
if (kc<48 || kc>55)
{
return false;
}
}
}
function hncheck(event)
{
var kc;
if(window.event)
{
kc=event.keyCode;
}
else
{
kc=event.which;
}
if (kc>=65 && kc<=70)
{
return true;
}
else if (kc>=97 && kc<=102)
{
return true;
}
else if (kc>=48 && kc<=57)
{
return true;
}
else if (kc==8)
{
return true;
}
else if (kc==0)
{
return true;
}
else
{

return false;
}
}
</script><!-- Script by hscripts.com -->

  
HTML Code


<!--Script by hscripts.com-->
<!-- Free javascripts @ https://www.hscripts.com -->
<!-- Script by hscripts.com -->
<form>
<table>
<tr><td align="right">
Decimal:</td><td><input type="text" name="deci" id="deci" onkeyup="dconvert()" onkeypress="return dncheck(event,deci)"></td></tr>
<tr><td align="right">Binary:</td><td><input type="text" name="bin" id="bin" onkeyup="bconvert(this.value)" onkeypress="return bncheck(event,bin)"></td></tr>
<tr><td align="right">Octal:</td><td><input type="text" name="oct" id="oct" onkeyup="oconvert(this.value)" onkeypress="return oncheck(event,oct)"></td></tr>
<tr><td align="right">Hexadecimal:</td><td><input type="text" name="hdeci" id="hdeci" onkeyup="hconvert(this.value)" onkeypress="return hncheck(event)"></td></tr>
</table>
</form>
&copy;<a href="https://www.hscripts.com">hscripts.com</a><!-- Script by hscripts.com -->

  • Release Date - 07-04-2008
  • Get free version without ©copyright link for just $10/-
  • For customization of this script or any script development, mail to support@hscripts.com

Usage

  • Copy the HTML code given above to create form
  • Copy the above Javascript code into your page and use it.
  • If the number entered in the textbox, The javascript converts the base and displays the number in binary,decimal,octal,hexadecimal format.

License

Related Scripts

Free Javascript Scripts


Ask Questions

Ask Question