While waiting for your solutions I came up with the following idea:
Use sprintf() to print the float or double to an array of char. Parse this to get the exponent and mantissa. Code looks now the following way:
void printDoubleToChar(double d, char* c){
char valAsChar[256];
sprintf(valAsChar, "%.12e", d);
int expStart = 0, expWidth = 0;
for(int i=0; i<sizeof(valAsChar); i++){
if(valAsChar[i] == 'e'){
expStart = i+1;
break;
}
}
for(int i=expStart; i<sizeof(valAsChar); i++){
if(valAsChar[i] == '\0'){
expWidth = i - expStart;
break;
}
}
char chValExp[32];
memcpy(chValExp, &valAsChar[expStart], expWidth+1);
char chValMan[128];
memcpy(chValMan, valAsChar, expStart-1);
chValMan[expStart-1] = '\0';
sprintf(c, "%s*10^{%s}", chValMan, chValExp);
}
int main(){
double myNewDbl = 3.95743e-5;
char chDbl[256];
printDoubleToChar(myNewDbl, chDbl);
printf("\nchDbl: %s\n", chDbl); // output: chDbl: 3.957430000000*10^{-05}
}
But honestly, I prefer the much simpler solution by dasblinkenlight :)
Thank you all for your help!
Alex