Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Decimal To binary Recursive conversion java?

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Decimal To binary Recursive conversion java?

    My conversion is wrong there is an additional zero i would be very grateful if somebody could help me figure out what is wrong with my code i know it would be easier having one method for this but i want to use both a private method that takes a string buffer and an int and a public method that takes an int . code is below
    public class DecToBin
    {
    public static void main (String [] args)
    {
     
    System.out.println (" the value of 10 in binary is " + decimalToBinary (10));
    System.out.println (" the value of 1 in binary is " + decimalToBinary (1));
    System.out.println (" the value of 2 in binary is " + decimalToBinary (2));
    System.out.println (" the value of 5 in binary is " + decimalToBinary (5));
    System.out.println (" the value of -5 in binary is " + decimalToBinary (-5));
     
     
    }
     
    private static String decimalToBinary(int decimal, StringBuffer buffer)
    {
    buffer.append(decimal % 2);
    decimal /= 2;
    if (decimal == 0) return buffer.reverse().toString();
    else return decimalToBinary(decimal, buffer);
    }
    public static String decimalToBinary (int decimal)
    {
    StringBuffer buffer = new StringBuffer ();
    if (decimal < 0)
    return " - " + decimalToBinary (decimal* -1);
    else return decimalToBinary (decimal, buffer.append(decimal %2));
    }
    }


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Decimal To binary Recursive conversion java?

    Coz of your buffer.append(decimal%2) statement in
    public static String decimalToBinary (int decimal)
    {
    StringBuffer buffer = new StringBuffer ();
    if (decimal < 0)
    return " - " + decimalToBinary (decimal* -1);
    else return decimalToBinary (decimal, buffer.append(decimal %2));
    }
    It's also appending the zero in buffer this time.

Similar Threads

  1. Decimal to binary conversion
    By baueml01 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 22nd, 2011, 06:42 AM
  2. Java API for RTF to DOCX Conversion
    By saravananiyyanar in forum Java SE APIs
    Replies: 0
    Last Post: December 31st, 2010, 07:43 AM
  3. [SOLVED] decimal to binary problem
    By mick.bhattarai in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 26th, 2010, 07:48 PM
  4. JAVA to C++ conversion
    By hackerboy101 in forum Java Theory & Questions
    Replies: 1
    Last Post: April 28th, 2010, 08:31 PM
  5. binary conversion..
    By chronoz13 in forum Java Theory & Questions
    Replies: 8
    Last Post: September 16th, 2009, 10:47 AM

Tags for this Thread