0

I want to convert int to char array and double to char array. Any way of doing this in C or Objective C.

3
  • 2
    Do you mean something along the lines of turning an integer into a string using snprintf, or do you mean something else? Commented Aug 31, 2010 at 14:32
  • Are you trying to accomplish something or learn how to do something? Either is possible here. Commented Aug 31, 2010 at 14:37
  • What I want is if int is converted to char then the resulting char array should be of 4 byte (assuming int size 4 byte) and same for the double. Commented Aug 31, 2010 at 14:43

2 Answers 2

3

If you want to treat the number as an array of char, you can take the address and cast the pointer:

int i;
double d;
char * ic = (char *) &i;
char * dc = (char *) &d;

then ic and dc are pointers to char. They aren't zero-terminated, so you can't use them as strings, but they can be used as arrays.

Sign up to request clarification or add additional context in comments.

Comments

2

For another interpretation of what "converting" means, use

union double_or_bytes { double d ; char bytes[8] ; } converter;

converter.d = <the double you have> ;

<do what you wanted to do with> converter.bytes ;

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.