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

Results 1 to 10 of 10

11242004, 06:58 PM #1
 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; 11242004 at 07:00 PM.
11242004, 07:06 PM
#2
 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.
11242004, 07:10 PM
#3
 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:
That's called debugging. If you're still having problems post back (a hint: the ASCII table).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; }
shmoove
11242004, 07:32 PM
#4
 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.
11242004, 08:48 PM
#5
 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; 11242004 at 09:03 PM.
11242004, 11:36 PM
#6
 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
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?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; }
11252004, 05:23 AM
#7
 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.
11252004, 05:41 AM
#8
 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.
11252004, 07:08 AM
#9
 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
08282012, 07:29 AM
#10
 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;
}