...

View Full Version : binary to decimal conversion in java



cwl157
11-24-2004, 06:58 PM
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.


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

turbowrx
11-24-2004, 07:06 PM
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.

shmoove
11-24-2004, 07:10 PM
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:


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

turbowrx
11-24-2004, 07:32 PM
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.

cwl157
11-24-2004, 08:48 PM
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?

cwl157
11-24-2004, 11:36 PM
ok i am really close here is the method i have


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?

turbowrx
11-25-2004, 05:23 AM
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.

cwl157
11-25-2004, 05:41 AM
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.

Roelf
11-25-2004, 07:08 AM
uhm, why not use:


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

}

gauravdeep Sing
08-28-2012, 07:29 AM
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;

}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum