/* This file takes a value strictly between 0 and 1 in floating point format and * converts it to a 20 bit hex integer (decimal immediately to the left of * the leftmost bit. */ #include #include main(int argc, char *argv[]){ float convert; int exp, i; unsigned int result = 0, mant; if (argc < 2) { printf ("Usage: floattohex \n"); exit (-1); } convert = (float)atof(argv[1]); exp = (*(unsigned int *)&convert) & 0x7F800000; mant = (*(unsigned int *)&convert) & 0x007FFFFF; exp = exp >> 23; exp = exp - 127; result |= 1 << (20 + exp); for (i = 0; i < (20 + exp); i++) if (mant & (1 << (23 - i))) result |= 1 << (20 + exp - i); printf ("%05X\n", result); }