Convert Binary Number

[Total: 0    Average: 0/5]

This form will convert binary to decimal and vice versa.

Head Code:

<script LANGUAGE="JavaScript">

<!--

function d2b(form) { // Decimal to Binary

	j = form.decimal.value;

        for (i=0; i<8; ++i) {

		l = eval("form.a"+i);

             	if (j & 1) 

                	l.status=true;

              	else

                    	l.status=false;

		      	j >>>= 1;

	}

}



function b2d(form) { // Binary to Decimal

	j=0;

        for (i=7; i>=0; --i) {

		j <<= 1;

		l = eval("form.a"+i);

              	if (l.status) 

                	j |= 1;

	}

        form.decimal.value=j;

}

// -->

</script>

Body Code:

<form>

  <p>Binary: <input TYPE="checkbox" NAME="a7" onClick="b2d(this.form)" value="ON"> <input

  TYPE="checkbox" NAME="a6" OnClick="b2d(this.form)" value="ON"> <input TYPE="checkbox"

  NAME="a5" OnClick="b2d(this.form)" value="ON"> <input TYPE="checkbox" NAME="a4"

  OnClick="b2d(this.form)" value="ON"> <input TYPE="checkbox" NAME="a3"

  OnClick="b2d(this.form)" value="ON"> <input TYPE="checkbox" NAME="a2"

  OnClick="b2d(this.form)" value="ON"> <input TYPE="checkbox" NAME="a1"

  OnClick="b2d(this.form)" value="ON"> <input TYPE="checkbox" NAME="a0"

  OnClick="b2d(this.form)" value="ON"> <br>

  Decimal: <input TYPE="text" NAME="decimal" OnChange="d2b(this.form)" size="20"> </p>

</form>

Times Viewed: 3

Leave a Reply

Your email address will not be published. Required fields are marked *