codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  how to use recursion to convert to binary....  masterchief at 01:56 on Thursday, February 08, 2007
 

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????????

  Re: how to use recursion to convert to binary....  mmarab at 09:23 on Thursday, February 08, 2007
 

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!

  Re: how to use recursion to convert to binary....  masterchief at 12:10 on Thursday, February 08, 2007
 

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

  Re: how to use recursion to convert to binary....  crwood at 23:36 on Thursday, February 08, 2007
 


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);
}
}
}









CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  What`s wrong with this code?
•  Re: problem to populate tables dynamically
•  passing data using multiple form styles
•  Re: beginnner looping help
•  Re: how to use recursion to convert to binary....
•  time taking to render menu
•  How to measure a Web Page`s Size through javascript?
•  Re: array copy
•  Can javascript preload swf files?


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2007