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


Code:
struct bcd_number 
{ 
unsigned int length; 
unsigned int fractional; 
signed char sign; 
unsigned char *digits; 
};


And thats the double to BCD function:

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->digits = malloc (512);


    char* z = (char*) bcd->digits; 




    sprintf (tmp,"%g",fabs(before));


    bcd->length = strlen(tmp); 
    bcd->sign = (before < 0) ? '-' : '+';


    for (size_t i=0; i<bcd->length; ++i)
     { *z++ = tmp[i] - '0'; } 


    sprintf (tmp,"%g",fabs(fractional)); 


    for (size_t i = strlen(tmp)-1; i!=0; --i) 
    if (tmp[i] != '0') 
    { tmp[i+1] = 0; break; } 




    bcd->fractional = strlen(tmp+2);
    bcd->length += bcd->fractional; 




    for (char* t = tmp + 2; *t; *z++ = *t++ - '0'); 
        bcd->digits = realloc (bcd->digits, bcd->length); 






    return bcd; 

}

That works perfect.


And I had also added the ability to preform addition/subtraction (Complete source code: [C] BCD - Pastebin.com) but now I want to preform multiplication and division. But the problem is that there are only chars as digits (I dont 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?