# Base Conversion

• February 14th, 2010, 11:06 AM
Pingu
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 :)

Code :

```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); }       }```
• February 14th, 2010, 03:10 PM
Shambolic
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.
• February 15th, 2010, 01:33 AM
jassi
Re: Base Conversion
Hi..Where is your main() method. How can your run a program without a main() function??
• February 16th, 2010, 01:08 PM
CT&SC
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.
• February 16th, 2010, 04:26 PM
helloworld922
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.

Code :

`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`
• February 16th, 2010, 04:34 PM
CT&SC
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.
• February 22nd, 2011, 03:10 PM
exnuke1972
Re: Base Conversion
Just a correction on the automated method that java provides for this.

Code :

`parseInt(String s, int radix)`

for more visit the api
• February 22nd, 2011, 03:15 PM
JavaPF
Re: Base Conversion
This thread is over a year old.