/* * SPDX-FileCopyrightText: 2020 Bruce Cowan <bruce@bcowan.me.uk> * SPDX-License-Identifier: Apache-2.0 */ #include <inttypes.h> #include <math.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #define DOUBLE_TO_BYTES(d) ((uint8_t *) &(d)) #define BYTES_TO_DOUBLE(b) (*((double *) (b))) int main (void) { double pi = M_PI; uint8_t *bytes = DOUBLE_TO_BYTES (pi); printf ("Bytes are ["); for (int i = 0; i < 7; i++) printf ("%02" PRIx8 ", ", bytes[i]); printf ("%02" PRIx8 "]\n", bytes[7]); double d = BYTES_TO_DOUBLE (bytes); printf ("Converting back, the value is %f\n", d); return 0; }