Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Binary to Decimal conversion

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 10-13-2012, 03:18 AM PM User | #1 gabsillis New Coder   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
10-13-2012, 04:29 AM   PM User | #2
Logic Ali
Senior Coder

Join Date: Sep 2010
Location: London
Posts: 1,026
Thanks: 0
Thanked 206 Times in 201 Posts
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.

Quote:
 Code: `if (binary.charAt(i) === 1)`
=== is the wrong operator because you are comparing an integerr to a character.

Try again.

 10-13-2012, 04:46 AM PM User | #3 Old Pedant Supreme Master coder!     Join Date: Feb 2009 Posts: 24,949 Thanks: 75 Thanked 4,307 Times in 4,274 Posts Is there any point in noting that JavaScript will do this for you? Code: `````` The second argument to parseInt -- the 2 -- tells JS to convert the string in radix=2. That is, in binary. 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.
10-13-2012, 05:25 AM   PM User | #4
Logic Ali
Senior Coder

Join Date: Sep 2010
Location: London
Posts: 1,026
Thanks: 0
Thanked 206 Times in 201 Posts
Quote:
 Originally Posted by Old Pedant Is there any point in noting that JavaScript will do this for you?
I think the point was to replicate the algorithm used by parseInt.

 10-13-2012, 11:23 PM PM User | #5 Old Pedant Supreme Master coder!     Join Date: Feb 2009 Posts: 24,949 Thanks: 75 Thanked 4,307 Times in 4,274 Posts 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.
 10-15-2012, 02:32 PM PM User | #6 gabsillis New Coder   Join Date: Sep 2012 Posts: 15 Thanks: 4 Thanked 0 Times in 0 Posts fixed 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
 10-15-2012, 03:03 PM PM User | #7 gabsillis New Coder   Join Date: Sep 2012 Posts: 15 Thanks: 4 Thanked 0 Times in 0 Posts 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
10-15-2012, 03:14 PM   PM User | #8
Logic Ali
Senior Coder

Join Date: Sep 2010
Location: London
Posts: 1,026
Thanks: 0
Thanked 206 Times in 201 Posts
Quote:
 Originally Posted by gabsillis fixed! it works completely!
OK, now validate the input.

 10-17-2012, 09:31 PM PM User | #9 gabsillis New Coder   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 Internet-explorer. Code: ``` Untitled Document ```
 10-17-2012, 10:14 PM PM User | #10 Logic Ali Senior Coder     Join Date: Sep 2010 Location: London Posts: 1,026 Thanks: 0 Thanked 206 Times in 201 Posts 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 (10-18-2012)
 01-21-2013, 06:02 PM PM User | #11 dexter28 New to the CF scene   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 7-binary 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.
 01-21-2013, 06:42 PM PM User | #12 Philip M Supreme Master coder!     Join Date: Jun 2002 Location: London, England Posts: 17,473 Thanks: 200 Thanked 2,469 Times in 2,447 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 __________________ 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. Last edited by Philip M; 01-21-2013 at 07:51 PM..
01-21-2013, 09:49 PM   PM User | #13
felgall
Master Coder

Join Date: Sep 2005
Location: Sydney, Australia
Posts: 6,088
Thanks: 0
Thanked 579 Times in 569 Posts
Quote:
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/

Beginners need to advise whether they want to learn "Latin" JavaScript for Netscape 3 or "Italian" JavaScript for modern browsers.

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 06:01 PM.