Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 13 of 13
Thread: Binary to Decimal conversion

10132012, 02:18 AM #1
 Join Date
 Sep 2012
 Posts
 15
 Thanks
 4
 Thanked 0 Times in 0 Posts
Binary to Decimal conversion
Hello all,
I'm making a script that converts a binary input into a decimal number.
I always get a 0 as answer for some reason.
Code:var countMult = 1; var binary = prompt("enter a binary number"); var len = binary.length; var dec = 0; var convert = function(){ for (i=0;i<len;i++){ if (binary.charAt(i) === 1){ //this part seems to be a problem dec+=countMult; } countMult = countMult*2; console.log(dec); } }; convert();
10132012, 03:29 AM
#2
You are trying to perform the calculation from left to right, but it should be right to left, in other words, start at the end of the string.
=== is the wrong operator because you are comparing an integerr to a character.Code:if (binary.charAt(i) === 1)
Try again.
10132012, 03:46 AM
#3
Is there any point in noting that JavaScript will do this for you?
The second argument to parseInt  the 2  tells JS to convert the string in radix=2. That is, in binary.Code:<script type="text/javascript"> var binary = "110010111011000011101"; var number = parseInt( binary, 2 ); alert( number ); </script>
And of course you can go back the other way:
Code:var number = 7181711; var binary = number.toString(2);
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
10132012, 04:25 AM
#4
10132012, 10:23 PM
#5
Yeah, you are probably right. But in that case the code should be checking for and rejecting any characters other than "1" and "0".
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
10152012, 01:32 PM
#6
 Join Date
 Sep 2012
 Posts
 15
 Thanks
 4
 Thanked 0 Times in 0 Posts
fixed
it works nowCode:var countMult = 1; var binary = prompt("enter a binary number"); var len = binary.length; var dec = 0; var convert = function(){ for (i=0;i<len;i++){ if (binary.charAt(i) == 1){ dec+=countMult; } countMult = countMult*2; console.log(dec); } }; convert();
but I still need to reverse the order
It should go in reverse order now but it fails to calculate it correctly...Code:var countMult = 1; var binary = prompt("enter a binary number"); var len = binary.length; var dec = 0; var convert = function(){ for (i=0;i<len;i++){ if (binary.charAt(leni) == 1){ dec+=countMult; } countMult = countMult*2; console.log(dec); } }; convert();
still working with it
Last edited by gabsillis; 10152012 at 01:52 PM.
10152012, 02:03 PM
#7
 Join Date
 Sep 2012
 Posts
 15
 Thanks
 4
 Thanked 0 Times in 0 Posts
fixed! it works completely!Code:var countMult = 1; var binary = prompt("enter a binary number"); var len = binary.length; var dec = 0; var convert = function(){ for (i=0;i<len;i++){ if (binary.charAt(len(i+1)) == 1){ dec+=countMult; } countMult = countMult*2; console.log(dec); } }; convert();
10152012, 02:14 PM
#8
10172012, 08:31 PM
#9
 Join Date
 Sep 2012
 Posts
 15
 Thanks
 4
 Thanked 0 Times in 0 Posts
I incorporated the binary script into a list of calculator functions to be called upon with buttons in html. The only problem with it is that something I am using is not compatible with Internetexplorer.
Code:<!DOCTYPE html PUBLIC "//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <script> var add = function(){ var one = prompt("enter first number"); one = parseInt(one,10); var two = prompt("enter second number"); two = parseInt(two,10); alert(one+two); return one+two; }; var subtract = function(){ var one = prompt("enter first number"); one = parseInt(one,10); var two = prompt("enter second number"); alert(onetwo); return onetwo; }; var multipltwo = function(){ var one = prompt("enter first number"); one = parseInt(one,10); var two = prompt("enter second number"); two = parseInt(two,10); alert(one*two); return one*two; }; var divide = function(){ var one = prompt("enter first number"); one = parseInt(one,10); var two = prompt("enter second number"); two = parseInt(two,10); alert(one/two); return one/two; }; var power = function(){ var one = prompt("enter base number"); one = parseInt(one,10); var two = prompt("enter exponent number"); two = parseInt(two,10); if(two===0){ alert(1); return 1; } else{ alert(one*two(one,two1)); return one*two(one,two1); } }; //different part of calculator var countMult = 1; var dec = 0; var convert = function(){ var binartwo = prompt("enter a binartwo number *note antwo number greater than 1 will be seen as a 0*"); var len = binartwo.length; for (i=0;i<len;i++){ if (binartwo.charAt(len(i+1)) == 1){ dec+=countMult; } countMult = countMult*2; console.log(dec); } alert(dec); }; </script> <meta httpequiv="ContentType" content="text/html; charset=utf8" /> <title>Untitled Document</title> </head> <body> <input type="button" onClick="convert()" value="change a binary number to a decimal number"> </body> </html>
10172012, 09:14 PM
#10
Click on the yellow triangle and don't disable that dialog, at least not while testing.
Then you'll probably want to use Google.
Users who have thanked Logic Ali for this post:
gabsillis (10182012)
01212013, 05:02 PM
#11
 Join Date
 Jan 2013
 Posts
 1
 Thanks
 0
 Thanked 0 Times in 0 Posts
Can anyone help me do this please??
I've been stuck on this for a few weeks
The user enters 0010101 and 010100. These should be converted to decimal (21 and 20, respectively). The Finch will speak that: “The two numbers entered are 21 and 20. Their sum is 41 and when multiplied gives 420. Subtracting the two numbers gives 1”.
Please note that the program must *not* use a simple loop which tests for all 127 cases of decimal number in the case of the 7binary digit number etc. The program must incorporate a proper algorithm for converting the number by parsing (i.e. searching) the string of bits.
The program should be tested with binary numbers input from the keyboard as well as from a file. You may decide how the two binary numbers in the file are formatted, e.g., one after the other or separated with a new line.
01212013, 05:42 PM
#12
 Join Date
 Jun 2002
 Location
 London, England
 Posts
 18,140
 Thanks
 203
 Thanked 2,547 Times in 2,525 Posts
Sounds like homework! See forum Rule #1.5.
And please do not hijack someone else's thread. Prefer to start a thread of your own.
Hint  try using the search feature of this forum.
It is your responsibility to die() if necessary…..  PHP Manual
Last edited by Philip M; 01212013 at 06:51 PM.
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
01212013, 08:49 PM
#13
 Join Date
 Sep 2005
 Location
 Sydney, Australia
 Posts
 6,642
 Thanks
 0
 Thanked 649 Times in 639 Posts
What's worse  they've hijacked a JavaScript thread to ask a question which is obviously not intended to be JavaScript. We don't even know what language that the person is learning where they were given this homework question. Had they supplied the code they have written so far a moderator would at least have some idea of which forum the question belongs in so as to properly slit it off into its own thread.
Stephen
Learn Modern JavaScript  http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
Don't forget to start your JavaScript code with"use strict";
which makes it easier to find errors in your code.