title: C - Converting decimal number to packed BCD
body: <p>How can I achieve these conversions in C without using sprintf?</p>
  
  <pre><code>20 =&gt; 0x20
  12 =&gt; 0x12
  </code></pre>
  
  <p>Currently I have:</p>
  
  <pre><code>int year = 12;
  int month = 10;
  int day = 9;
  unsigned char date[3];
  
  date[0] = year &amp; 0xFF;
  date[1] = month &amp; 0xFF;
  date[2] = day &amp; 0xFF;
  </code></pre>
  
  <p>date will contain { 0x0C, 0x0A, 0x09 } but I want it to be { 0x12, 0x10, 0x09 }</p>