13

I am having an array of integer say int example[5] = {1,2,3,4,5}. Now I want to convert them into character array using C, not C++. How can I do it?

4
  • 2
    What does 1,2,3,4,5 stand for? What are some reasonable conversions you are expecting? Please add an example to your question. Commented Jan 17, 2011 at 11:46
  • Its an typo... its int example[5] Commented Jan 17, 2011 at 11:48
  • @winbros - char array in the sense you want it as {'1','2','3','4','5'} ? Commented Jan 17, 2011 at 11:51
  • Do you want to deal with ASCII of 1,2...? Commented Jan 17, 2011 at 11:55

5 Answers 5

11

Depending on what you really want, there are several possible answers to this question:

int example[5] = {1,2,3,4,5};
char output[5];
int i;

Straight copy giving ASCII control characters 1 - 5

for (i = 0 ; i < 5 ; ++i)
{
    output[i] = example[i];
}

characters '1' - '5'

for (i = 0 ; i < 5 ; ++i)
{
    output[i] = example[i] + '0';
}

strings representing 1 - 5.

char stringBuffer[20]; // Needs to be more than big enough to hold all the digits of an int
char* outputStrings[5];

for (i = 0 ; i < 5 ; ++i)
{
    snprintf(stringBuffer, 20, "%d", example[i]);
    // check for overrun omitted
    outputStrings[i] = strdup(stringBuffer);
}
Sign up to request clarification or add additional context in comments.

3 Comments

What if the integer number is larger than 9? Integers above 10 won't be converted to the char format.
Is there a solution without a for loop for the first option (ASCII control characters), or is this already the cleanest solution?
@ilja I can't think of one.
3
#include <stdio.h>

int main(void)
{
    int i_array[5] = { 65, 66, 67, 68, 69 };
    char* c_array[5];

    int i = 0;
    for (i; i < 5; i++)
    {   
        //c[i] = itoa(array[i]);    /* Windows */

        /* Linux */
        // allocate a big enough char to store an int (which is 4bytes, depending on your platform)
        char c[sizeof(int)];    

        // copy int to char
        snprintf(c, sizeof(int), "%d", i_array[i]); //copy those 4bytes

        // allocate enough space on char* array to store this result
        c_array[i] = malloc(sizeof(c)); 
        strcpy(c_array[i], c); // copy to the array of results

        printf("c[%d] = %s\n", i, c_array[i]); //print it
    }   

    // loop again and release memory: free(c_array[i])

    return 0;
}

Outputs:

c[0] = 65
c[1] = 66
c[2] = 67
c[3] = 68
c[4] = 69

1 Comment

Your answer will only work for numbers up to 999, after that it'll start truncating digits - INT_MAX for a 32 bit 2's complement integer is 2147483647 which as rather more than 3 digits (one of your four is taken with '\0')
1

You can convert a single digit-integer into the corresponding character using this expression:

int  intDigit = 3;
char charDigit = '0' + intDigit;  /* Sets charDigit to the character '3'. */

Note that this is only valid, of course, for single digits. Extrapolating the above to work against arrays should be straight-forward.

Comments

0

You need to create the array, because sizeof(int) is (almost surely) different from sizeof(char)==1.

Have a loop in which you do char_example[i] = example[i].


If what you want is to convert an integer into a string you could just sum your integer to '0' but only if you're sure that your integer is between 0 and 9, otherwise you'll need to use some more sophisticated like sprintf.

Comments

0

In pure C I would do it like this:

char** makeStrArr(const int* vals, const int nelems)
{
    char** strarr = (char**)malloc(sizeof(char*) * nelems);
    int i;
    char buf[128];

    for (i = 0; i < nelems; i++)
    {
        strarr[i] = (char*)malloc(sprintf(buf, "%d", vals[i]) + 1);
        strcpy(strarr[i], buf);
    }
    return strarr;
}

void freeStrArr(char** strarr, int nelems)
{
    int i = 0;
    for (i = 0; i < nelems; i++) {
        free(strarr[i]);
    }
    free(strarr);
}

void iarrtostrarrinc()
{
    int i_array[] = { 65, 66, 67, 68, 69 };
    char** strarr = makeStrArr(i_array, 5);
    int i;
    for (i = 0; i < 5; i++) {
        printf("%s\n", strarr[i]);
    }
    freeStrArr(strarr, 5);
}

1 Comment

A malloc-cast isn't 'pure C', its C++.

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.