tags:

views:

75

answers:

2

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

+2  A: 

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 ;
Pascal Cuoq
And `union int_or_bytes {int i; char bytes[4];};`
Nick T
+2  A: 

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.

David Thornley