title: How to manually convert decimal value to hexadecimal string in C?
body: <p><em>n.b. I know that this question has been asked on StackOverflow before in a variety of different ways and circumstances, but the search for the answer I seek doesn't quite help my specific case.  So while this initially looks like a duplicate of a question such as <a href="https://stackoverflow.com/questions/3464194/how-to-convert-integer-to-hexadecimal-in-c">How can I convert an integer to a hexadecimal string in C?</a> the answers given, are accurate, but not useful to me.</em></p>
  
  <p>My question is how to convert a decimal integer, into a hexadecimal string, manually.  I know there are some beat tricks with stdlib.h and printf, but this is a college task, and I need to do it manually (professor's orders).  We are however, permitted to seek help.</p>
  
  <p>Using the good old <em>"divide by 16 and converting the remainder to hex and reverse the values"</em> method of obtaining the hex string, but there must be a big bug in my code as it is not giving me back, for example "BC" for the decimal value "188".</p>
  
  <p>It is assumed that the algorithm will NEVER need to find hex values for decimals larger than 256 (or FF).  While the passing of parameters may not be optimal or desirable, it's what we've been told to use (although I am allowed to modify the getHexValue function, since I wrote that one myself).</p>
  
  <p>This is what I have so far:</p>
  
  <pre><code>/* Function to get the hex character for a decimal (value) between
   * 0 and 16.  Invalid values are returned as -1.
   */
  char getHexValue(int value) 
  {
     if (value &lt; 0) return -1;
     if (value &gt; 16) return -1;
     if (value &lt;= 9) return (char)value;
     value -= 10;
     return (char)('A' + value);
  }
  
  
  /* Function asciiToHexadecimal() converts a given character (inputChar) to
   * its hexadecimal (base 16) equivalent, stored as a string of
   * hexadecimal digits in hexString. This function will be used in menu
   * option 1.
   */
  void asciiToHexadecimal(char inputChar, char *hexString)
  {
     int i = 0;
     int remainders[2];
     int result = (int)inputChar;
     while (result) {
        remainders[i++] = result % 16;
        result /= (int)16;
     }
     int j = 0;
     for (i = 2; i &gt;= 0; --i) {
        char c = getHexValue(remainders[i]);
        *(hexString + (j++)) = c;
     }
  }
  </code></pre>
  
  <p>The <code>char *hexString</code> is the pointer to the string of characters which I need to output to the screen (eventually). The <code>char inputChar</code> parameter that I need to convert to hex (which is why I never need to convert values over 256).</p>
  
  <p>If there is a better way to do this, which still uses the <code>void asciiToHexadecimal(char inputChar, char *hexString)</code> function, I am all ears, other than that, my debugging seems to indicate the values are ok, but the output comes out like <code>\377</code> instead of the expected hexadecimal alphanumeric representation.</p>
  
  <p>Sorry if there are any terminology or other problems with the question itself (or with the code), I am still very new to the world of C.</p>
  
  <p><strong>Update:</strong>
  It just occurred to me that it might be relevant to post the way I am displaying the value in case its the printing, and not the conversion which is faulty.  Here it is:</p>
  
  <pre><code>char* binaryString = (char*) malloc(8);
  char* hexString = (char*) malloc(2);
  asciiToBinary(*(asciiString + i), binaryString);
  asciiToHexadecimal(*(asciiString + i), hexString);
  printf("%6c%13s%9s\n", *(asciiString + i), binaryString, hexString);
  </code></pre>
  
  <p>(Everything in this code snip-pit works except for <code>hexString</code>)</p>