2 <SCRIPT LANGUAGE=
"JavaScript">
4 ////////////////////////////////////////
5 ////////////////////////////////////////
7 // Copyright 2001 F. Permadi
8 ////////////////////////////////////////
9 ////////////////////////////////////////
12 ////////////////////////////////////////
14 ////////////////////////////////////////
17 clearTimeout(timerId);
20 ////////////////////////////////////////
21 // convert decimal to hexadecimal number
22 ////////////////////////////////////////
23 function binToDec(setTimer)
25 var binNum=document.form1.bin.value;
26 var decNum=decConvertFromBase(binNum, 2);
29 document.form1.dec.value=decNum;
32 timerId=setTimeout("binToDec(true)", 100);
38 document.form1.bin.value="";
43 ////////////////////////////////////////
44 // convert hexadecimal to decimal number
45 ////////////////////////////////////////
46 function hexToDec(setTimer)
48 var hexNum=document.form1.hex.value;
49 var decNum=decConvertFromBase(hexNum, 16);
52 document.form1.dec.value=decNum;
55 timerId=setTimeout("hexToDec(true)", 100);
61 document.form1.dec.value="";
66 ////////////////////////////////////////
67 // process decimal conversion
68 ////////////////////////////////////////
69 function hexProcess(setTimer)
71 var hexNum=""+document.form1.hex.value;
73 // make sure the number is valid
74 for (i=0; i<hexNum.length; i++)
76 if ((hexNum.charAt(i)>='0' && hexNum.charAt(i)<='9') ||
77 (hexNum.charAt(i)>='a' && hexNum.charAt(i)<='f') ||
78 (hexNum.charAt(i)>='A' && hexNum.charAt(i)<='F'))
83 alert(hexNum+" is not a valid HEXADECIMAL number because it contains "+hexNum.charAt(i));
84 document.form1.hex.value="";
92 timerId=setTimeout("hexProcess(true)", 100);
96 ////////////////////////////////////////
97 // process decimal conversion
98 ////////////////////////////////////////
99 function decProcess(setTimer)
101 var num=document.form1.dec.value;
102 // make sure the number is valid
103 for (i=0; i<num.length; i++)
105 if (num.charAt(i)>='0' && num.charAt(i)<='9')
110 alert(num+" is not a valid DECIMAL number because it contains\n \""+num.charAt(i)+"\"");
111 document.form1.dec.value="";
119 timerId=setTimeout("decProcess(true)", 100);
124 ////////////////////////////////////////
125 // process binary conversion
126 ////////////////////////////////////////
127 function binProcess(setTimer)
129 var num=document.form1.bin.value;
130 // make sure the number is valid
131 for (i=0; i<num.length; i++)
133 if (num.charAt(i)>='0' && num.charAt(i)<='1')
138 alert(num+" is not a valid BINARY number because it contains\n \""+num.charAt(i)+"\"");
139 document.form1.bin.value="";
147 timerId=setTimeout("binProcess(true)", 100);
153 ////////////////////////////////////////
154 // convert decimal to hexadecimal number
155 ////////////////////////////////////////
156 function decToBin(setTimer)
158 var decNum=document.form1.dec.value;
159 var binNum=decConvertToBase(decNum, 2);
162 document.form1.bin.value=binNum;
165 timerId=setTimeout("decToBin(true)", 100);
171 document.form1.bin.value="";
176 ////////////////////////////////////////
177 // convert decimal to hexadecimal number
178 ////////////////////////////////////////
179 function decToHex(setTimer)
181 var decNum=document.form1.dec.value;
182 var hexNum=decConvertToBase(decNum, 16);
185 document.form1.hex.value=hexNum;
188 timerId=setTimeout("decToHex(true)", 100);
194 document.form1.hex.value="";
200 ////////////////////////////////////////
201 // convert non decimal number to decimal
202 ////////////////////////////////////////
203 function decConvertFromBase(num, base)
208 for (i=num.length-1; i>=0; i--)
210 var digit=num.charAt(i);
211 if (digit>='a' || digit>='A')
213 if (digit=='a' || digit=='A')
215 else if (digit=='b' || digit=='B')
217 else if (digit=='c' || digit=='C')
219 else if (digit=='d' || digit=='D')
221 else if (digit=='e' || digit=='E')
223 else if (digit=='f' || digit=='F')
226 decNum+=digit*Math.pow(base, power);
233 ////////////////////////////////////////
234 // convert decimal number to non decimal
235 ////////////////////////////////////////
236 function decConvertToBase(num, base)
243 result=Math.floor(num/base);
262 // just append the next remainder to the beginning of the string
263 newNum=""+remainder+newNum;
268 ////////////////////////////////////////
269 // increase decimal val and recalc others
270 ////////////////////////////////////////
271 function incDecNumber()
273 if(document.form1.dec.value >= 0) {
274 document.form1.dec.value++;
275 document.form1.hex.value = decConvertToBase(document.form1.dec.value, 16);
276 document.form1.bin.value = decConvertToBase(document.form1.dec.value, 2);
281 ////////////////////////////////////////
282 // decrease decimal val and recalc others
283 ////////////////////////////////////////
284 function decDecNumber()
286 if(document.form1.dec.value > 0) {
287 document.form1.dec.value--;
288 document.form1.hex.value = decConvertToBase(document.form1.dec.value, 16);
289 document.form1.bin.value = decConvertToBase(document.form1.dec.value, 2);
295 document.form1.dec.value = '';
296 document.form1.hex.value = '';
297 document.form1.bin.value = '';
302 <b>Number Conversion Applet
</b><br>
306 <td>Decimal:
</td><td><INPUT TYPE=
"text" NAME=
"dec" SIZE=
"20" onBlur=
"javascript:clearAuto()" onFocus=
"javascript:decProcess(true)" onChange=
"javascript:clearAuto()" style=
"text-align:right;"></td>
308 <td>Binary:
</td><td><INPUT TYPE=
"text" NAME=
"bin" SIZE=
"20" onBlur=
"javascript:clearAuto()" onFocus=
"javascript:binProcess(true)" onChange=
"javascript:clearAuto()" style=
"text-align:right;"></td>
310 <td>Hex:
</td><td><INPUT TYPE=
"text" NAME=
"hex" SIZE=
"20" onBlur=
"javascript:clearAuto()" onFocus=
"javascript:hexProcess(true)" onChange=
"javascript:clearAuto()" style=
"text-align:right;"></td>
314 <button onClick='incDecNumber();'
>+
</button>
315 <button onClick='decDecNumber();'
>-
</button>
316 <button onClick='clearAll();'
>Clear
</button>