title: Conversion from byte (binary) to ASCII in C
body: <p>I'm working with a PIC microprocessor, and I need to send values from an AD-Converter to a terminal (in this case HyperTerminal). Now, the terminal only understands ASCII. The value the AD-Converter gives a single byte (so ranging from 0 to 255). How can convert this binary number to ASCII?</p>
  
  <p>To be entirely clear, the byte is binary. So <code>0100 0000</code> should result in <code>64</code>. Ofcourse this would mean needing to send 2 ASCII numbers to the pc.</p>
  
  <p><strong>Edit:</strong> Apparently I'm not entirely clear here. I need to have the ASCII-code of the binary number in binary as well so I can send it through a USB-template provided by the chip-manufacturer.</p>
  
  <p><strong>Edit 2:</strong> After some more delving into other topics, <a href="https://electronics.stackexchange.com/a/65489/33807">this</a> answer led me to trying out <code>itoa()</code> and <code>utoa()</code>. Now, <code>itoa()</code> works, but unfortunately is for unsigned charachters. <code>utoa()</code> would be for unsigned chars, but that doesn't work. Here's an example of what should be able to happen:</p>
  
  <pre><code>char USB_In_Buffer[64];
  char myValue = 0x55;
  
  itoa(myValue, USB_In_Buffer);
  putUSBUSART(USB_In_Buffer, 3);
  </code></pre>
  
  <p>So every ASCII character should be sent to <code>USB_In_Buffer</code>. Then the total of characters written to this buffer should be used as second parameter in <code>putUSBUSART()</code>. I only need to convert numbers to ASCII as well, so not the whole characterset needs to be implemented.</p>
  
  <p><strong>Edit 3:</strong> Because some automated functions do not appear to be supported, I thought it wouldn't be that hard to make my own function.</p>
  
  <pre><code>if(ADCONvalue/100 != 0){
      res++;
      USB_In_Buffer[0] = (ADCONvalue / 100) + 0x30;
  }
  if(ADCONvalue/10 != 0){
      res++;
      if(res == 1){
          USB_In_Buffer[0] = (ADCONvalue / 10) + 0x30;
      }else{
          USB_In_Buffer[1] = (ADCONvalue / 10) + 0x30;
      }   
  
  }
  res++;
  if(res == 1){
      USB_In_Buffer[0] = ADCONvalue % 10 + 0x30;
  }else if(res == 2){
      USB_In_Buffer[1] = ADCONvalue % 10 + 0x30;
  }else{
      USB_In_Buffer[2] = ADCONvalue % 10 + 0x30;
  }
  putUSBUSART(USB_In_Buffer, res);
  </code></pre>
  
  <p>But there seems to be an error somewhere. Small numbers do work (0-99), but for some reason when there should be <code>121</code>, it displays <code>1&lt;1</code>. For larger numbers, all the chars are non-numeric.</p>
  
  <p><strong>Edit 4:</strong> After rechecking my code I found the error. For one, <code>ADCONvalue</code> was a <code>char</code>, not an <code>unsigned char</code>. Also I forgot to do <code>%10</code> for the tenths. Current working code:</p>
  
  <pre><code>if(ADCONvalue/100 != 0){
      res++;
      USB_In_Buffer[0] = (ADCONvalue / 100) + 0x30;
  }
  if(ADCONvalue/10 != 0){
      res++;
      if(res == 1){
          USB_In_Buffer[0] = (ADCONvalue / 10)%10 + 0x30;
      }else{
          USB_In_Buffer[1] = (ADCONvalue / 10)%10 + 0x30;
      }   
  
  }
  res++;
  if(res == 1){
      USB_In_Buffer[0] = ADCONvalue % 10 + 0x30;
  }else if(res == 2){
      USB_In_Buffer[1] = ADCONvalue % 10 + 0x30;
  }else{
      USB_In_Buffer[2] = ADCONvalue % 10 + 0x30;
  }
  putUSBUSART(USB_In_Buffer, res);
  </code></pre>