/* * simple hacked-up program to show (in hexadecimal) the bit representation * of a floating-point number. * bytes are printed in memory order, which for little-endian architectures * is least-significant first. */ #include #include void show_bytes(FILE * out, const void *bytes, const size_t sz); int main(int argc, char *argv[]) { if (argc < 2) { fprintf(stderr, "usage %s number\n", argv[0]); return EXIT_FAILURE; } char * endptr; double d = strtod(argv[1], &endptr); if (*endptr != '\0') { fprintf(stderr, "not numeric\n"); return EXIT_FAILURE; } float f = d; fprintf(stdout, "input value %g\n", d); fprintf(stdout, "as double: "); show_bytes(stdout, &d, sizeof d); fprintf(stdout, "\n"); fprintf(stdout, "as float: "); show_bytes(stdout, &f, sizeof f); fprintf(stdout, "\n"); return EXIT_SUCCESS; } void show_bytes(FILE * out, const void *bytes, const size_t sz) { unsigned char *uchar = (unsigned char *) bytes; for (size_t i = 0; i < sz; ++i) { fprintf(out, "%02x ", uchar[i]); } }