Program to covert number from one base to another like binary, octal, hexadecimal or decimal to binary, octal, hexadecimal or decimal
Syntax for Program to covert number from one base to another like binary, octal, hexadecimal, decimal in Javascript
<html>
<head>
<title>Number Convertor</title>
<script language="JavaScript">
<!--
function NumConvertor() {
var Num = ''
Num = Num2Dec(document.frmNumConvertor.FromVal.value, RadioNumVal(document.frmNumConvertor.FromNum))
Num = Dec2Num(Num, RadioNumVal(document.frmNumConvertor.ToNum))
document.frmNumConvertor.ToVal.value = Num
}
function RadioNumVal(RadioGroup) {
for (var i = 0; i < RadioGroup.length; i++) {
if (RadioGroup[i].checked == true) { return RadioGroup[i].value; }
}
}
function Num2Dec(Val, Num) {
return parseInt(Val, Num);
}
function Dec2Num(Val, Num) {
var cChars = '0123456789ABCDEF';
var str = ''while (Val > 0) {
var str = cChars.charAt(Val % Num) + str;
var Val = Math.floor(Val / Num);
}
return str
}
//-->
</script>
<style>
body, td, input
{
font-family: arial;
color: #000066;
font-size: 11px;
}
fieldset
{
width: 350px;
padding: 2px 10px 5px 10px;
font-size: 13px;
}
</style>
</head>
<body bgcolor="#F6CEE3">
<form name="frmNumConvertor">
<fieldset style="">
<legend><b>Number Convert</b></legend>
<table width="350" cellpadding="4" cellspacing="4" border="0">
<tr>
<td style="padding-top: 10px">
<b>From</b><br>
<input type="text" name="FromVal" value="" style="width: 150px;">
</td>
<td style="padding-top: 10px">
<b>To</b><br>
<input type="text" name="ToVal" value="" style="width: 150px;"readonly>
</td>
</tr>
<tr>
<td valign="bottom">
<table>
<tr>
<td>
<input type="radio" name="FromNum" value="2"checked>Binary
</td>
</tr>
<tr>
<td>
<input type="radio" name="FromNum" value="10">Decimal
</td>
</tr>
<tr>
<td>
<input type="radio" name="FromNum" value="8">Octal
</td>
</tr>
<tr>
<td>
<input type="radio" name="FromNum" value="16">Hexadecimal
</td>
</tr>
</table>
</td>
<td valign="bottom">
<table>
<tr>
<td>
<input type="radio" name="ToNum" value="2">Binary
</td>
</tr>
<tr>
<td>
<input type="radio" name="ToNum" value="10"checked>Decimal
</td>
</tr>
<tr>
<td>
<input type="radio" name="ToNum" value="8">Octal
</td>
</tr>
<tr>
<td>
<input type="radio" name="ToNum" value="16">Hexadecimal
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="2">
<input type="button" value="Convert" onclick="NumConvertor()">
</td>
</tr>
</table>
</fieldset>
</form>
</body>
</html>