# Convert int array to char array ?

This is a discussion on Convert int array to char array ? within the C Programming forums, part of the General Programming Boards category; Hi all, I would like to ask about how could i convert efficiently an array of int to an array ...

1. ## Convert int array to char array ?

Hi all,

I would like to ask about how could i convert efficiently an array of int to an array of char.

Example:

int intArray[3] = {0x12e45c78, 0xa453f4b4, 0xf7a4ka74};

char chArray[8*4]; // because each int is 4 char.

Now, how can i proceed to convert so?

Thanks a lot.

2. If you want a char pointer to your array, you can simply do
Code:
```int intArray[3];

char *charArray;

charArray=(char *)intArray;```

Hi all,

I would like to ask about how could i convert efficiently an array of int to an array of char.

Example:
Code:
```  int intArray[3] = {0x12e45c78, 0xa453f4b4, 0xf7a4ka74};

char chArray[8*4]; // because each int is 4 char.```
Now, how can i proceed to convert so?

Thanks a lot.
That is going to depend a whole lot on what you want to do with the character array...

If you're intending to convert integer values to their text representations you will need more than 4 bytes for each (probably 10) and you will need to use library calls such as sprintf() to do the conversion.

If the final result is to be some form of numeric storage you need to know that moving a 4 byte binary value to 4 1 byte values can get seriously broken in lots of different ways, not the least of which is that each of the numbers you've shown is way bigger than the 0x7f limit of a 1 byte character.

I think we need more information on what you are trying to accomplish before we can give you good advice....

4. Hi,

Yes, i would like to convert in order to have a storage of char array that holds all the data in was in the int array.

Hope this is enough to answer.

Thanks a lot.

5. Well if you're just looking to write the array to a file, you could do as giove suggested and take a pointer to the array and typecast it as type char.

6. If you want your code to be portable, you'd better use sizeof() instead of directly inputing your type's size. int may or may not be 4 bytes for example.

7. The approaches here are good. Also be aware of the memcpy function - useful if you need to save a copy of the data rather than just work with it using different typed pointers.