Search code examples
c++bitconverters

How to convert bytes from uint64_t to double?


I have binary file from which I read uint64_t val (using little-endian). Now I want to convert this uint64_t to double (not just casting, but exactly the number that would be double if I entered it from the file). So the should has the same bit representation. How I should do this?


Solution

  • Simple portable way:

    uint64_t u64 = read_from_file();
    double d;
    memcpy(&d, &u64, sizeof(d));
    

    Most compilers will generate just a couple of instructions for this.