Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a byte array into double in C?

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 how to convert it?

like image 480
beta Avatar asked Jan 02 '12 16:01

beta


1 Answers

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
like image 124
Adam Zalcman Avatar answered Oct 21 '22 18:10

Adam Zalcman