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. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    7

    Smile 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.
    Last edited by ahmedBanihammad; 11-08-2010 at 12:10 AM.

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    34
    If you want a char pointer to your array, you can simply do
    Code:
    int intArray[3];
    
    char *charArray;
    
    charArray=(char *)intArray;

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ahmedBanihammad View Post
    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. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    7
    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. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,616
    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.
    Devoted my life to programming...

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  2. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 11:36 AM
  3. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21