Topic: Decimal to Binary Conversion (Page 1 of 1) |
|
---|---|
Obsessive-Compulsive (I) Inmate From: |
posted 06-13-2006 07:04
Having some trouble with this one. code: <html> <head> <script language="Javascript" type="text/javascript"> function ConvertBinary() { var originalnumber = 0; var resultnumber = 0; var resulstring = ""; if (document.form1.dropdown.value == "dectobin") { originalnumber = document.form1.original1.value; for (i = 1000; i > 1; i--) { if (( originalnumber - Math.pow(2,i) > 0) { resultstring+="1" } if (( originalnumber - Math.pow(2,i) < 1{ resultstring+="0" } } document.form1.result1.value=resultstring; } } error(1): wrong syntax </script> </head> <body> <center> <form name="form1" onsubmit="return false;"> Input:<input type="text" name="original1" value=""> Output:<input type="text" name="result1"><p> <select name="dropdown"> <option value="dectobin">Decimal to Binary <option value="bintodec">Binary to Decimal </select><br> <input type="button" onClick="ConvertBinary();" value="Convert Number!"> </center> </form> </body> </html>
|
Paranoid (IV) Inmate From: Norway |
posted 06-13-2006 09:15
[quickie] code: function ConvertBinary() { if (document.form1.dropdown.value == "dectobin") { var srcBase = 10, dstBase = 2 } else { var srcBase = 2, dstBase = 10 } var originalValue = document.form1.original1.value.replace( /^\s+|\s+$/, '' ), originalNumber = parseInt( originalValue, srcBase ); if( isNaN(originalNumber) ) // /!\ that safety test should be improved var resultString = 'invalid input' else var resultString = originalNumber.toString( dstBase ) document.form1.result1.value= resultString; }
|
Obsessive-Compulsive (I) Inmate From: |
posted 06-13-2006 23:11
Sorry poi, code: <html> <head> <title>TME 3 Part 2</title> <script language="Javascript" type="text/javascript"> function ConvertBinary() { if (document.form1.dropdown.value == "dectobin") { var inBase = 10, outBase = 2 } if (document.form1.dropdown.value == "bintohex") { var inBase = 2, outBase = 6 } else { var inBase = 2, outBase = 10 } var originalValue = document.form1.original1.value var originalNumber = parseInt( originalValue, inBase ); if( isNaN(originalNumber) ) var resultString = 'invalid input' else var resultString = originalNumber.toString( outBase ) document.form1.result1.value= resultString; } </script> </head> <body> <center> <form name="form1" onsubmit="return false;"> Input:<input type="text" name="original1" value=""> Output:<input type="text" name="result1"><p> <select name="dropdown"> <option value="dectobin">Decimal to Binary <option value="bintodec">Binary to Decimal <option value="bintohex">Binary to Hex </select><br> <input type="button" onClick="ConvertBinary(this.form);" value="Convert Number!"> </center> </form> </body> </html> |
Obsessive-Compulsive (I) Inmate From: |
posted 06-13-2006 23:14
Nevermind. Realized Hex is base 16. Changed to 16 and worked perfectly. |
Paranoid (IV) Inmate From: Norway |
posted 06-14-2006 10:30 |