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 8 of 8

Thread: Base Conversion

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Base Conversion

    I'm trying to write a program that inputs a number and a base and converts that number to a base. I have written the following which compiles, but doesn't seem to do the right thing :S Any help would be much appreciated

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.*;
     
    public class Question
    {
    int inputDec,base,converted;
     
    public int Conversion()
     
      {
        InputStreamReader stream = new InputStreamReader(System.in);
         BufferedReader reader = new BufferedReader(stream);
     
     
    try
     
    {
     
    int n,b;
    System.out.print("Enter number:");
    n = Integer.parseInt(reader.readLine());
    System.out.print("Which base would you like to convert to?");
    b = Integer.parseInt(reader.readLine());
    converted=toBase(n,b);
    System.out.println(converted);
     
    }
     
    catch(IOException e)
     
        {return converted;
     
        }
     
        return converted;
    }
     
    public int toBase(int n,int b)
    {
            if (n !=0)
            {
                return(n%b);
            }
            else
                    return (0);
    }
     
     
     
    }


  2. #2
    Member
    Join Date
    Jan 2010
    Location
    Oxford, UK
    Posts
    30
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default Re: Base Conversion

    % is the "modulo" operator. For integers a and b, a % b is the remainder when a is divided by b. Some examples:

    8 % 3 = 2
    11 % 5 = 1
    n % 2 = 1 for n odd
    n % 2 = 0 for n even

    And so on...
    Obviously this is not what you want your program to do.

  3. #3
    Member
    Join Date
    Feb 2010
    Location
    Dehradun, India
    Posts
    37
    Thanks
    1
    Thanked 7 Times in 6 Posts

    Default Re: Base Conversion

    Hi..Where is your main() method. How can your run a program without a main() function??

  4. #4
    Member
    Join Date
    Feb 2010
    Location
    Auburn, AL
    Posts
    31
    Thanks
    0
    Thanked 8 Times in 8 Posts

    Default Re: Base Conversion

    Yeah, returning n % b is just going to give the remainder after division by b. Useful for converting bases, but not enough.

    Think about it like this. A way to get a number's base ten representation is to divide by powers of 10 and take the remainder after division by 10. This gives every digit. What do I mean? Consider the base ten representation of the number one thousand three hundred and twenty-four.

    Dividing by 1 gives one thousand three hundred and twenty-four. The remainder after division of this is four. Therefore, the one's digit of the number is a 4.

    Dividing by 10 gives one hundred thirty-two. The remainder after division by 10 is two, so the 10's digit is 2.

    Dividing by 100 gives thirteen. The remainder after division by 10 is three, so the 100's digit is 3.

    Dividing by 1000 gives 1. The remainder after division by 10 is 1, so the 1000's digit is 1.

    Dividing by 10000 gives 0, and the algorithm stops at this stage. The representation you have constructed is the base ten representation of the number.

    You might be thinking that it should work the same for hexadecimal, just use 16 instead of 10. This is correct. Just make sure you map digits greater than 9 to the corresponding letters of the alphabet, a, b, c, d, e, and f.
    Let me know what you think of my website:
    http://www.auburn.edu/~carpept
    Comments or suggestions are appreciated!

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Base Conversion

    Integers in Java don't distinguish between bases (actually, none of the primitive data types do). In order to display the different representations you must use strings. To calculate what digit comes next:

    1. modulate your current number by the base. That result is the digit to put at the beginning of the string (so, any digits calculated before go after this digit)
    2. divide your current number by the base (you will get truncation, but that's ok because we want it here). This is your new current number.
    3. repeat until your current number is 0.

    Alternatively, you can use Java's Integer class to do this conversion for you.

    Integer.toString(10,16); // the first number is the value, the second is the radix or base. I think this is limited from 2-16
    Last edited by helloworld922; February 16th, 2010 at 04:29 PM.

  6. #6
    Member
    Join Date
    Feb 2010
    Location
    Auburn, AL
    Posts
    31
    Thanks
    0
    Thanked 8 Times in 8 Posts

    Default Re: Base Conversion

    ^ Yeah that's pretty much what I said. Of course, if the exercise isn't understanding how to do it, the built-in function is to be preferred.
    Let me know what you think of my website:
    http://www.auburn.edu/~carpept
    Comments or suggestions are appreciated!

  7. #7
    Junior Member
    Join Date
    Jan 2011
    Location
    Jackson, MI
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Base Conversion

    Just a correction on the automated method that java provides for this.

    parseInt(String s, int radix)

    for more visit the api

  8. #8
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Base Conversion

    This thread is over a year old.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Explicit Conversion?
    By chronoz13 in forum Java Theory & Questions
    Replies: 3
    Last Post: November 11th, 2009, 11:40 PM
  2. numerical conversion methods..
    By chronoz13 in forum Java SE APIs
    Replies: 12
    Last Post: September 27th, 2009, 04:29 AM
  3. binary conversion..
    By chronoz13 in forum Java Theory & Questions
    Replies: 8
    Last Post: September 16th, 2009, 10:47 AM