How to convert a byte array into double in C?

后端 未结 3 918
野的像风
野的像风 2020-12-03 17:34

I\'ve got a byte array containing 8 bytes and would like to convert and use them as a double precision binary floating-point number.

Could someone please tell me ho

3条回答
  •  囚心锁ツ
    2020-12-03 18:22

    Try this:

    double a;
    memcpy(&a, ptr, sizeof(double));
    

    where ptr is the pointer to your byte array. If you want to avoid copying use a union, e.g.

    union {
      double d;
      char bytes[sizeof(double)];
    } u;
    // Store your data in u.bytes
    // Use floating point number from u.d
    

提交回复
热议问题