功能要较强的数字进制转换的脚本

- 中国WEB开发者网络 (http://www.webasp.net)
-- 网页特效 (http://www.webasp.net/javascript/)
--- 功能要较强的数字进制转换的脚本 (http://www.webasp.net/javascript/1/401.htm)
-- 发布日期: 2005-06-07
<!-- 网页特效代码由[中国WEB开发者网络:http://www.ChinaWebDev.com]提供! -->
<!-- 要实现此效果需要 2 个步骤: -->

<!-- 第 1 步: -->
<!-- 把下面的代码加到<HEAD></HEAD>区域中: -->

<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
var hex = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F");
function CKparseInt(n, r) {
for (var i = 0; i < n.length; ++i)
if (n.charAt(i) >= r) {
alert("格式错");
return 0;
}
if (isNaN(M = parseInt(n, r)))
alert ("格式错");
return M;
}
// decimal to any other base 2 to 16
function DecimaltoAnother(A, radix) {
s = "";
while (A >= radix) {
s += hex[A % radix]; // remainder
A = Math.floor(A / radix); // quotient, rounded down
}
return transpose(s += hex[A]);
}
// string reversal
function transpose(s) {
N = s.length;
for (i = 0,t = ""; i < N; i++)
t += s.substring(N-i-1, N-i); // s.substring(from, to)
return t;
}
// convert item.value using radix
function EvalAny(item, r) {
M = CKparseInt(item.value, r); // check this one
for (var i = 0, MyForm = document.forms[0]; i < MyForm.length; ++i) // re-evaluate all
MyForm.elements[i].value = DecimaltoAnother(M, MyForm.elements[i].name.substr(1,3));
}
// End -->
</script>




<!-- 第 2 步: -->
<!-- 把下面的代码加到<BODY></BODY>区域中: -->

<center>
<h3>进制转换</h3>
<br>
<form method="post">
<table border=0 align=center>
<tr>
<td align=right>
<p>二进制:</p>
</td>
<td><input name="b002" value="0" onChange="EvalAny(this, 2)" size=27></td>
</tr>
<tr>
<td align=right>三进制:</td>
<td><input name="t003" value="0" onChange="EvalAny(this, 3)" size=21></td>
</tr>
<tr>
<td align=right>五进制:</td>
<td><input name="q005" value="0" onChange="EvalAny(this, 5)" size=16></td>
</tr>
<tr>
<td align=right>八进制:</td>
<td><input name="o008" value="0" onChange="EvalAny(this, 8)" size=12></td>
</tr>
<tr>
<td align=right>十进制:</td>
<td><input name="d010" value="0" onChange="EvalAny(this, 10)" size=11></td>
</tr>
<tr>
<td align=right>十六进制:</td>
<td><input name="h016" value="0" onChange="EvalAny(this, 16)" size=8></td>
</tr>
</table>
</form>
</center>



webasp.net