Ideveloper
Ideveloper

Reputation: 1463

byte array to double

I have a four byte char array and I want to convert it to double. How can I achieve this in C.

Upvotes: 1

Views: 1051

Answers (1)

wallyk
wallyk

Reputation: 57784

float and double are so closely related in C that an explicit conversion is probably not needed. However, it would be necessary for transmission to another system, or to match a data format specification. This will do what you ask:

union {
    char   c [4];
    float  f;
} x;
double d;

memcpy (x.c, character_source, sizeof x.c);
d = x.f;

Upvotes: 2

Related Questions