Waiting for your decisions, I came up with the following idea:
Use sprintf () to print a float or double into a char array. Disassemble this to get the exponent and mantissa. The code is as follows:
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);
But to be honest, I prefer the much simpler dasblinkenlight solution :)
Thank you all for your help!
Alex
source share