Removed bit flip buttons from interface.
[CS-101.git] / binary_counter.html
blobb0e33258897e7ad25bfe0b98e24b54879396225f
1 <html>
2 <SCRIPT LANGUAGE="JavaScript">
3 <!--
4 ////////////////////////////////////////
5 ////////////////////////////////////////
6 // F Permadi 10/10/01
7 // Copyright 2001 F. Permadi
8 ////////////////////////////////////////
9 ////////////////////////////////////////
10 var timerId;
12 ////////////////////////////////////////
13 // clear timer
14 ////////////////////////////////////////
15 function clearAuto()
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);
27 if (binNum!="-1")
29 document.form1.dec.value=decNum;
30 if (setTimer==true)
32 timerId=setTimeout("binToDec(true)", 100);
34 return true;
36 else
38 document.form1.bin.value="";
39 return false;
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);
50 if (decNum!="-1")
52 document.form1.dec.value=decNum;
53 if (setTimer==true)
55 timerId=setTimeout("hexToDec(true)", 100);
57 return true;
59 else
61 document.form1.dec.value="";
62 return false;
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'))
81 else
83 alert(hexNum+" is not a valid HEXADECIMAL number because it contains "+hexNum.charAt(i));
84 document.form1.hex.value="";
85 return false;
88 hexToDec();
89 decToBin();
90 if (setTimer==true)
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')
108 else
110 alert(num+" is not a valid DECIMAL number because it contains\n \""+num.charAt(i)+"\"");
111 document.form1.dec.value="";
112 return false;
115 decToBin(false);
116 decToHex(false);
117 if (setTimer==true)
119 timerId=setTimeout("decProcess(true)", 100);
121 return true;
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')
136 else
138 alert(num+" is not a valid BINARY number because it contains\n \""+num.charAt(i)+"\"");
139 document.form1.bin.value="";
140 return false;
143 binToDec(false);
144 decToHex(false);
145 if (setTimer==true)
147 timerId=setTimeout("binProcess(true)", 100);
149 return true;
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);
160 if (binNum!="-1")
162 document.form1.bin.value=binNum;
163 if (setTimer==true)
165 timerId=setTimeout("decToBin(true)", 100);
167 return true;
169 else
171 document.form1.bin.value="";
172 return false;
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);
183 if (hexNum!="-1")
185 document.form1.hex.value=hexNum;
186 if (setTimer==true)
188 timerId=setTimeout("decToHex(true)", 100);
190 return true;
192 else
194 document.form1.hex.value="";
195 return false;
197 return true;
200 ////////////////////////////////////////
201 // convert non decimal number to decimal
202 ////////////////////////////////////////
203 function decConvertFromBase(num, base)
205 var decNum=0;
206 var power=0;
207 // loop backward
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')
214 digit=10;
215 else if (digit=='b' || digit=='B')
216 digit=11;
217 else if (digit=='c' || digit=='C')
218 digit=12;
219 else if (digit=='d' || digit=='D')
220 digit=13;
221 else if (digit=='e' || digit=='E')
222 digit=14;
223 else if (digit=='f' || digit=='F')
224 digit=15;
226 decNum+=digit*Math.pow(base, power);
227 power++;
229 return decNum;
233 ////////////////////////////////////////
234 // convert decimal number to non decimal
235 ////////////////////////////////////////
236 function decConvertToBase(num, base)
238 var newNum="";
239 var result=num;
240 var remainder=0;
241 while (result>0)
243 result=Math.floor(num/base);
244 remainder=num%base;
245 num=result;
247 if (remainder>=10)
249 if (remainder==10)
250 remainder='A';
251 if (remainder==11)
252 remainder='B';
253 if (remainder==12)
254 remainder='C';
255 if (remainder==13)
256 remainder='D';
257 if (remainder==14)
258 remainder='E';
259 if (remainder==15)
260 remainder='F';
262 // just append the next remainder to the beginning of the string
263 newNum=""+remainder+newNum;
265 return 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);
293 function clearAll()
295 document.form1.dec.value = '';
296 document.form1.hex.value = '';
297 document.form1.bin.value = '';
300 //-->
301 </SCRIPT>
302 <b>Number Conversion Applet</b><br>
303 <form name="form1">
304 <table>
305 <tr>
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>
307 </tr><tr>
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>
309 </tr><tr>
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>
311 </tr>
312 </table>
313 </form>
314 <button onClick='incDecNumber();'>+</button>
315 <button onClick='decDecNumber();'>-</button>
316 <button onClick='clearAll();'>Clear</button>
317 </html>