1

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

2
  • 1
    That 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? Commented Aug 31, 2010 at 5:50
  • 1
    There's more than one format of floating point number. Commented Aug 31, 2010 at 6:04

1 Answer 1

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.