|
hey everyone:
Im trying to figure out how to write some code to convert decimal to binary,octal, or hexadecimal. This has to be done using recursion.
Can anyone help me please????????
|
|
|
This is an example of recursion:
void myMethod( int counter)
{
if(counter == 0)
return;
else
{
System.out.println("hello" + counter);
myMethod(--counter);
System.out.println(""+counter);
return;
}
}
The rest should be simple enough, just google it!
|
|
|
public static String Conversion(int n1, int n2){
//This method will convert any decimal value into any other value
//that is given
String ConValue="";
String[] Value ={"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"}; //values for all the different types
if (n1==0){
return ConValue="";
}
else
return Conversion(n1/n2, n2);
}
this is what i have so far, but i dont know how to start getting the numbers from decimal to one of the values in the array
|
|
|
public class Recurse {
public static void main(String[] args) {
int n = 20;
String bits = toBinary(n, "");
System.out.println("toBinary(" + n + ") = " + bits);
System.out.println(n + " in binary = " + Integer.toBinaryString(n));
}
private static String toBinary(int n, String s) {
if(n == 0)
return s;
else {
s = String.valueOf(n % 2) + s;
// Either of these next two lines does the same thing.
n /= 2;
//n >>= 1;
//System.out.printf("n = %d s = %s%n", n, s);
return toBinary(n, s);
}
}
}
|
|
|
|
|
|
|
|
|
|