I have a four byte char array and I want to convert it to double. How can I achieve this in C.
Asked
Active
Viewed 1,046 times
1
-
1That entirely depends on what your four byte char array represents. Is it an integer? A fixed-point number? A floating-point number? In what format? – caf Aug 31 '10 at 05:50
-
1There's more than one format of floating point number. – caf Aug 31 '10 at 06:04
1 Answers
2
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;

wallyk
- 56,922
- 16
- 83
- 148