| « Which DD-WRT file to install | Searching file contents under Windows XP » |
Bandwidth Calculator
February 1st, 2008I was looking for an on-line Bandwidth Calculator to find out how many megabytes there were in 100 megabits. Simple I know but hay! I found quite a few, but unfortunately they were all rubbish! I was surprised how many bad implementations there were, some even submitted the form to the remote web server for it to work out. What a waste! Anyway after trawling the wastelands, I decided to write my own.
‘B’ indicates bytes and ‘b’ indicates bits so for example, if you want to find out how many megabytes of transfer you can get from a 100 megabit network card then:
- Enter 100 in the In unit box
- Select Mb from the In unit drop down
- Select MB from the Out unit drop down
Unfortunately, I have not figured out how to embed Javascript in a blog page, so here is the Bandwidth Calculator in action. For completeness the code is listed below. It travels with the standard licence - use it at your our risk yada-yada-yada!
PHP:
<html> | |
<body> | |
| |
<script language="Javascript"> | |
//<!-- | |
// This code was written by BigSoft Limited. | |
// Please include this message when you use the code in any form. | |
// (c) BigSoft Limited 2008 | |
// | |
function recalc() | |
{ | |
var frm = document.forms['converter']; | |
var in_val = frm.in.value; | |
var in_unit = frm.inunit.options[frm.inunit.selectedIndex].value; | |
var out_unit = frm.outunit.options[frm.outunit.selectedIndex].value; | |
| |
// convert in to bits | |
var bits; | |
switch (in_unit) | |
{ | |
case 'b' : bits = in_val ; break; | |
case 'B' : bits = in_val * 8 ; break; | |
case 'Kb': bits = in_val * 1024 ; break; | |
case 'KB': bits = in_val * 1024 * 8 ; break; | |
case 'Mb': bits = in_val * 1024 * 1024 ; break; | |
case 'MB': bits = in_val * 1024 * 1024 * 8 ; break; | |
case 'Gb': bits = in_val * 1024 * 1024 * 1024 ; break; | |
case 'GB': bits = in_val * 1024 * 1024 * 1024 * 8 ; break; | |
case 'Tb': bits = in_val * 1024 * 1024 * 1024 * 1024 ; break; | |
case 'TB': bits = in_val * 1024 * 1024 * 1024 * 1024 * 8; break; | |
} | |
| |
// convert bits to required unit | |
var answer; | |
switch (out_unit) | |
{ | |
case 'b' : answer = bits ; break; | |
case 'B' : answer = bits / 8 ; break; | |
case 'Kb': answer = bits / 1024 ; break; | |
case 'KB': answer = bits / 1024 / 8 ; break; | |
case 'Mb': answer = bits / 1024 / 1024 ; break; | |
case 'MB': answer = bits / 1024 / 1024 / 8 ; break; | |
case 'Gb': answer = bits / 1024 / 1024 / 1024 ; break; | |
case 'GB': answer = bits / 1024 / 1024 / 1024 / 8 ; break; | |
case 'Tb': answer = bits / 1024 / 1024 / 1024 / 1024 ; break; | |
case 'TB': answer = bits / 1024 / 1024 / 1024 / 1024 / 8; break; | |
} | |
frm.out.value = answer; | |
} | |
// --> | |
</script> | |
| |
<form name=converter> | |
<table> | |
<tr> | |
<td>In unit: <input name=in value=0 onChange="recalc()"> | |
<select name=inunit onChange="recalc()"> | |
<option value=b>b</option> | |
<option value=B>B</option> | |
<option value=Kb>Kb</option> | |
<option value=KB>KB</option> | |
<option value=Mb>Mb</option> | |
<option value=MB>MB</option> | |
<option value=Gb>Gb</option> | |
<option value=GB>GB</option> | |
<option value=Tb>Tb</option> | |
<option value=TB>TB</option> | |
</select> | |
| |
Out unit: <input name=out value=0 onFocus='blur()'> | |
<select name=outunit onChange="recalc()"> | |
<option value=b>b</option> | |
<option value=B>B</option> | |
<option value=Kb>Kb</option> | |
<option value=KB>KB</option> | |
<option value=Mb>Mb</option> | |
<option value=MB>MB</option> | |
<option value=Gb>Gb</option> | |
<option value=GB>GB</option> | |
<option value=Tb>Tb</option> | |
<option value=TB>TB</option> | |
</select> | |
</input></input></td> | |
</tr> | |
</table></form> | |
</body> | |
</html> |