Reputation: 81
I am trying to convert a base 10 number to any base by using conversion. Right now this is the code I have came up with. I have a sad feeling this may be completely wrong. The image below is an example of how this process should take place.
http://i854.photobucket.com/albums/ab107/tonytauart/rrrr.png
public static void main(String[] args) {
int base;
int number;
Scanner console = new Scanner(System.in);
System.out.println("Please enter the base");
base = console.nextInt();
System.out.println("Please enter the Number you would like to convert");
number = console.nextInt();
System.out.println(Converter(base, number));
}
public static int Converter(int Nbase, int Nnumber){
int answer;
int Rcontainer =0;
int cnt = 0;
int multiplier;
int temp;
double exp;
if(Nnumber/Nbase == 0){
cnt++;
exp = Math.pow(10,cnt);
multiplier = (int)exp;
answer = (Nnumber%Nbase)* multiplier + Rcontainer;
}
else
{
exp = Math.pow(10,cnt);
multiplier = (int)exp;
cnt++;
temp = Rcontainer;
Rcontainer = (Nnumber%Nbase)* multiplier + temp;
Nnumber = Nnumber/Nbase;
answer = Converter(Nbase,Nnumber);
}
return answer;
}
}
Upvotes: 8
Views: 42053
Reputation: 763
A quick way to do it in Java is this:
Integer.toString(int i,int radix);
For example,
Integer.toString(255,2)
would return "11111111". I'm not sure if you are just looking for a quick solution or do you actually want to implement the conversion method yourself. This would be a quick solution. Refer to this post: What is the method in the API for converting between bases?
Upvotes: 3
Reputation: 302
I just finished doing this problem for a comp sci class. I had to solve this recursively:
public static String convert(int number, int base)
{
int quotient = number / base;
int remainder = number % base;
if (quotient == 0) // base case
{
return Integer.toString(remainder);
}
else
{
return convert(quotient, base) + Integer.toString(remainder);
}
}
Upvotes: 14
Reputation: 11
public class Converter {
private static char symbols[] = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T' };
public static void main(String args[]) {
Converter converter = new Converter();
System.out.println(converter.convert(31, 16));
}
public String convert(int number, int base) {
return convert(number, base, 0, "");
}
private String convert(int number, int base, int position, String result) {
if (number < Math.pow(base, position + 1)) {
return symbols[(number / (int) Math.pow(base, position))] + result;
} else {
int remainder = (number % (int) Math.pow(base, position + 1));
return convert(number - remainder, base, position + 1, symbols[remainder / (int) (Math.pow(base, position))] + result);
}
}
}
Upvotes: 1
Reputation: 242
public class Converter {
private static char symbols[] = new char[] { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T' };
public static void main ( String args[] )
{
Converter converter = new Converter ();
System.out.println( converter.convert ( 31, 16 ));
}
public String convert ( int number, int base )
{
return convert(number, base, 0, "" );
}
private String convert ( int number, int base, int position, String result )
{
if ( number < Math.pow(base, position + 1) )
{
return symbols[(number / (int)Math.pow(base, position))] + result;
}
else
{
int remainder = (number % (int)Math.pow(base, position + 1));
return convert ( number - remainder, base, position + 1, symbols[remainder / (int)( Math.pow(base, position) )] + result );
}
}
}
This will convert from Base 2 to Base 36, although you could expand it by adding more symbols.
Upvotes: 10
Reputation: 25297
If you are just trying to convert bases (like to base 2), try the following code:
Integer.parseInt(Integer.toString(numberToConvert,base))
For specifically base 2:
Integer.parseInt(Integer.toBinaryString(numberToConvert))
Integer contains other methods such as toHexString that can be used. These assume that numberToConvert
is in base 10.
Upvotes: 2