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 in java
 User Name Remember Me? Password

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 11-24-2004, 07:58 PM PM User | #1 cwl157 New Coder   Join Date: Jun 2003 Posts: 92 Thanks: 0 Thanked 0 Times in 0 Posts binary to decimal conversion in java I need to write a method that converts a binary number to a decimal. Here is what i have so far any suggestions on how i can get this to work right now it returns 147.0 when you enter the binary number 11 which should have the decimal equivlant of 3. Does anyone know where i messed up? I've been working on just this method for probably 8 hours including yesterday and today and i dont think im even close. Any help is appreciated. Code: ```static double toDecimal (String s) { int l = s.length(); double result = 0; for (int i = 0; i < l; i++) { result = result + s.charAt(i) * Math.pow(2, (s.length() - i - 1)); } return result; }``` Last edited by cwl157; 11-24-2004 at 08:00 PM..
 11-24-2004, 08:06 PM PM User | #2 turbowrx New Coder   Join Date: Nov 2004 Posts: 54 Thanks: 0 Thanked 0 Times in 0 Posts 1) I would check the string to make sure you just have 0's or 1's 2) In your loop you should check to see if the char you are looking at is a zero or one, if zero, add zero, if 1, raise 2 to the index of the char, with everything starting at zero Hope this helped.
 11-24-2004, 08:10 PM PM User | #3 shmoove Regular Coder   Join Date: Dec 2003 Posts: 367 Thanks: 0 Thanked 0 Times in 0 Posts I can see the error, but instead of just pointing it out I'll save the 8 hours you'll waste next time you run into a problem: Code: ```static double toDecimal (String s) { int l = s.length(); double result = 0; System.out.println("s=" + s); System.out.println("l=" + l); for (int i = 0; i < l; i++) { System.out.println("result before iteration=" + result); System.out.println("s.charAt(i)=" + (double)s.charAt(i)); System.out.println("s.length() - i - 1=" + (s.length() - i - 1)); System.out.println("Math.pow(2, (s.length() - i - 1))=" + Math.pow(2, (s.length() - i - 1))); System.out.println("adding " + (s.charAt(i) * Math.pow(2, (s.length() - i - 1))) + " to result"); result = result + s.charAt(i) * Math.pow(2, (s.length() - i - 1)); System.out.println("result after iteration=" + result); } System.out.println("final result=" + result); return result; }``` That's called debugging. If you're still having problems post back (a hint: the ASCII table). shmoove
 11-24-2004, 08:32 PM PM User | #4 turbowrx New Coder   Join Date: Nov 2004 Posts: 54 Thanks: 0 Thanked 0 Times in 0 Posts In reference to my last post, my statement assumed you had no spaces in your string. If you do, you need to trim them off. Just check the string api for use of the method.
 11-24-2004, 09:48 PM PM User | #5 cwl157 New Coder   Join Date: Jun 2003 Posts: 92 Thanks: 0 Thanked 0 Times in 0 Posts I think i found the error. I am importing into the method a string then i am multiplying the string by a number so java takes the decimal equivlant found on the ascII chart and multiplies that instead of the string. how do i convert the number of the math.pow() method to a string. OK i found something called Double.toString(double) could i set math.pow(whatever) to a variable and then just use teh Double.toString(double) to make it a string and then would that correctly add like its supposed to? Last edited by cwl157; 11-24-2004 at 10:03 PM..
 11-25-2004, 12:36 AM PM User | #6 cwl157 New Coder   Join Date: Jun 2003 Posts: 92 Thanks: 0 Thanked 0 Times in 0 Posts ok i am really close here is the method i have Code: ```static double toDecimal (String s) { int l = s.length(); int result = 0; double num_1 = Double.parseDouble(s); for (int i = 0; i < l; i++) { int power = 1; for (int p = 1; p <= i; p++) { power = power * 2; System.out.println(power); } if (s.charAt(i) == '1') { result = result + power; } } return result; }``` it works except for when i enter a number such as 001 where it returns 4 instead of 1. I realize it is calculating it backwards. Does anyone know why it does this?
 11-25-2004, 06:23 AM PM User | #7 turbowrx New Coder   Join Date: Nov 2004 Posts: 54 Thanks: 0 Thanked 0 Times in 0 Posts 001 is 4. I tried your code out and it seems to return the correct answer, although you will have problems with strings that contain spaces or other characters than 0 or 1. You should add some checking in for that as well. I wrote a version that does that, but you shouldn't have any problem with that. I'm not sure I see where your problem is right now. You are not using the variable num_1, so you might as well get rid of that.
 11-25-2004, 06:41 AM PM User | #8 cwl157 New Coder   Join Date: Jun 2003 Posts: 92 Thanks: 0 Thanked 0 Times in 0 Posts yea after many hours i figured it out and i did add a method that checks to make sure it is a legal binary number.
 11-25-2004, 08:08 AM PM User | #9 Roelf Senior Coder   Join Date: Jun 2002 Location: Zwolle, The Netherlands Posts: 1,120 Thanks: 2 Thanked 31 Times in 31 Posts uhm, why not use: Code: ```public int bin2int(String str){ // do some validation upon str here // raise an error if it is not a valid binary number // or check for the NumberFormatExecption while calling the parseint method return (Integer.parseInt(str,2)); }``` __________________ I am the luckiest man in the world
 08-28-2012, 08:29 AM PM User | #10 gauravdeep Sing New to the CF scene   Join Date: Aug 2012 Posts: 1 Thanks: 0 Thanked 0 Times in 0 Posts Use this this will work.You were multiplying power with ask value of char.I turned char into string then to double. static double toDecimal (String s) { int l = s.length(); double result = 0; int por = 0; for (int i = 0; i < l; i++) { result = result + Double.parseDouble(String.valueOf(s.charAt(i))) * Math.pow(2, (s.length() - i - 1)); } return result; }

 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 07:37 PM.