Topic: Decimal to Binary Conversion (Page 1 of 1) Pages that link to <a href="http://ozoneasylum.com/backlink?for=28070" title="Pages that link to Topic: Decimal to Binary Conversion (Page 1 of 1)" rel="nofollow" >Topic: Decimal to Binary Conversion <span class="small">(Page 1 of 1)</span>\

 
Hutch
Obsessive-Compulsive (I) Inmate

From:
Insane since: May 2006

IP logged posted posted 06-13-2006 07:04 Edit Quote

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>



Not quite sure what the problem is,<html>

(Edited by Hutch on 06-13-2006 07:04)

poi
Paranoid (IV) Inmate

From: Norway
Insane since: Jun 2002

IP logged posted posted 06-13-2006 09:15 Edit Quote

[quickie]

there's a typo in the declaration of resultstring
there's one too many ) in the first if( originalNumber ...
there's one too too many ( and one too few ) few in the next if( originalNumber ...

sorry but are you sure you wrote/read that code carefully ?!

Anyway, base conversions are better done with the built-in functions ( parseInt and toString ), i.e :

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;
}



And your HTML markup could be improved too
The language attribute and CENTER tag are deprecated. You could/should use label tags around your FORM fields. Oh, and adding a DOCTYPE would be nice too.

Hope that helps,

[/quickie]



(Edited by poi on 06-13-2006 10:18)

Hutch
Obsessive-Compulsive (I) Inmate

From:
Insane since: May 2006

IP logged posted posted 06-13-2006 23:11 Edit Quote

Sorry poi,
I wrote this code at 3.30am when my eyes were not seeing so straight.
Thanks for the help, I have made a few adjustments.

Does javascript have a built in function for binary -> hex? i.e. dstBase = 6 ( I think ).

I tried adding a part to do that but to no avail.

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>

Hutch
Obsessive-Compulsive (I) Inmate

From:
Insane since: May 2006

IP logged posted posted 06-13-2006 23:14 Edit Quote

Nevermind. Realized Hex is base 16. Changed to 16 and worked perfectly.

poi
Paranoid (IV) Inmate

From: Norway
Insane since: Jun 2002

IP logged posted posted 06-14-2006 10:30 Edit Quote





Post Reply
 
Your User Name:
Your Password:
Login Options: Remember Me On This Computer
 
Your Text:
Loading...
Options: Show Signature
Enable Slimies
Enable Linkwords

« BackwardsOnwards »

Show Forum Drop Down Menu