Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-24-2004, 06:58 PM   PM User | #1
cwl157
New Coder

 
Join Date: Jun 2003
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
cwl157 is an unknown quantity at this point
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 07:00 PM..
cwl157 is offline   Reply With Quote
Old 11-24-2004, 07:06 PM   PM User | #2
turbowrx
New Coder

 
Join Date: Nov 2004
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
turbowrx is an unknown quantity at this point
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.
turbowrx is offline   Reply With Quote
Old 11-24-2004, 07:10 PM   PM User | #3
shmoove
Regular Coder

 
Join Date: Dec 2003
Posts: 367
Thanks: 0
Thanked 0 Times in 0 Posts
shmoove is an unknown quantity at this point
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
shmoove is offline   Reply With Quote
Old 11-24-2004, 07:32 PM   PM User | #4
turbowrx
New Coder

 
Join Date: Nov 2004
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
turbowrx is an unknown quantity at this point
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.
turbowrx is offline   Reply With Quote
Old 11-24-2004, 08:48 PM   PM User | #5
cwl157
New Coder

 
Join Date: Jun 2003
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
cwl157 is an unknown quantity at this point
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 09:03 PM..
cwl157 is offline   Reply With Quote
Old 11-24-2004, 11:36 PM   PM User | #6
cwl157
New Coder

 
Join Date: Jun 2003
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
cwl157 is an unknown quantity at this point
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?
cwl157 is offline   Reply With Quote
Old 11-25-2004, 05:23 AM   PM User | #7
turbowrx
New Coder

 
Join Date: Nov 2004
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
turbowrx is an unknown quantity at this point
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.
turbowrx is offline   Reply With Quote
Old 11-25-2004, 05:41 AM   PM User | #8
cwl157
New Coder

 
Join Date: Jun 2003
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
cwl157 is an unknown quantity at this point
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.
cwl157 is offline   Reply With Quote
Old 11-25-2004, 07:08 AM   PM User | #9
Roelf
Senior Coder

 
Join Date: Jun 2002
Location: Zwolle, The Netherlands
Posts: 1,110
Thanks: 2
Thanked 28 Times in 28 Posts
Roelf is on a distinguished road
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));

}
Roelf is offline   Reply With Quote
Old 08-28-2012, 07: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
gauravdeep Sing is an unknown quantity at this point
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;

}
gauravdeep Sing is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 Jump


All times are GMT +1. The time now is 02:50 AM.


Advertisement
Log in to turn off these ads.