title: Using logarithms or other method to convert from any base to another base in C
body: <p>I'm trying to write a C program to convert a number given a base, to any other base (Eg. base 2 binary number to base 8, or base 3 to base 16 hex). I've researched this quite a bit, and already read through a similar article, <a href="https://cs.stackexchange.com/questions/10318/the-math-behind-converting-from-any-base-to-any-base-without-going-through-base">The math behind converting from any base to any base without going through base 10?</a>] but in the explanation given, they utilize an array, which I am not allowed to do for this program. </p>
  
  <p>Without writing a lengthy program with a method for each possible combination of base conversion, I don't see how this is possible without an array to store possible values. I do understand that there is a change of base formula with logs that allows me to change between number bases, but I am unclear how I would apply this, as this formula only gives a decimal number answer, which I would still need to convert.</p>
  
  <pre><code>int log_base_n(int n, int logof)
  {
      double logBaseN = log10((double) logof) / log10((double) n);
      return (int) ceil(logBaseN);
  }
  </code></pre>
  
  <p>Here is my binary to decimal conversion which I am trying to use as an intermediate step:</p>
  
  <pre><code>/**
  * Convert decimal numbers to binary. Uses a greedy subtraction
  * algorithm. Assumes max integer allowed is 2 to 16 power.
  *
  * @param numberToConvert
  */
  void decToBin(int numberToConvert)
  {
  int power = 16;
  double ans = pow(2, power);
  
  if (numberToConvert &gt; ans)
  {
      printf("ERROR: Number too large to convert!\n");
      return;
  }
  
  while (ans &gt; numberToConvert)
  {
      power--;
      ans = pow(2, power);
  }
  
  printf("%d", 0);
  
  int i = power;
  
  while (i &gt;= 0)
  {
  
      ans = pow(2, i);
      numberToConvert = numberToConvert - ans;
      printf("%d", 1);
      i--;
  
      while ((pow(2, i) &gt; numberToConvert) &amp;&amp; (i &gt;= 0))
      {
          printf("%d", 0);
          i--;
          ans = pow(2, i);
      }
     }
  }
  </code></pre>
  
  <p>I know Java has a <code>parseInt()</code> method, that does base conversions, but is there something similar I can implement in C without having to write methods for each possible conversion like the one above, while still utilizing a logarithm related idea? Any help would be greatly appreciated.</p>