title: BCD arithmetic operations
body: <p>I have written a function that converts a double to a BCD (BCD: Save each digit of the double as an unsigned char, in addition save the complete length, the fractional length (part behind the floating point) and the sign of the double number).
  I use the following struct</p>
  <pre><code>struct bcd_number 
  { 
  unsigned int length; 
  unsigned int fractional; 
  signed char sign; 
  unsigned char *digits; 
  }; 
  </code></pre>
  <p>And thats the double to BCD function:</p>
  <pre><code>    struct bcd_number* double2bcd(double x) 
  { 
      char tmp[512]; 
      struct bcd_number* bcd = malloc (sizeof(struct bcd_number)); 
      
      int a = x;  
      double before = a;
      double fractional;
      fractional = x-(long)x;
  
   
      bcd-&gt;digits = malloc (512);
   
      char* z = (char*) bcd-&gt;digits; 
  
  
      sprintf (tmp,&quot;%g&quot;,fabs(before));
     
      bcd-&gt;length = strlen(tmp); 
      bcd-&gt;sign = (before &lt; 0) ? '-' : '+';
      
      for (size_t i=0; i&lt;bcd-&gt;length; ++i)
       { *z++ = tmp[i] - '0'; } 
  
      sprintf (tmp,&quot;%g&quot;,fabs(fractional)); 
  
      for (size_t i = strlen(tmp)-1; i!=0; --i) 
      if (tmp[i] != '0') 
      { tmp[i+1] = 0; break; } 
  
  
      bcd-&gt;fractional = strlen(tmp+2);
      bcd-&gt;length += bcd-&gt;fractional; 
  
  
      for (char* t = tmp + 2; *t; *z++ = *t++ - '0'); 
          bcd-&gt;digits = realloc (bcd-&gt;digits, bcd-&gt;length); 
  
  
  
      return bcd; 
  } 
  </code></pre>
  <p>That works perfect.</p>
  <p>And I had also added the ability to perform addition/subtraction (Complete source code: <a href="http://pastebin.com/HR70VukM" rel="nofollow noreferrer">http://pastebin.com/HR70VukM</a>) but now I want to perform multiplication and division.
  But the problem is that there are only chars as digits (I don't want to change that). I now that must be like 'multiplication on the paper' (classical way without calculator) but I have the idea that it must be like addition with the modulo operator. On the other hand I have no idea how to implement it with chars with modulo. Any ideas or hints?</p>