-
convert int to char
hi,
i've got to integers, lets say 0 and 6, which i want to copy into an character array. how do i do this?
char copied[3];
//copied[0] should be 0
//copied[1] should be 6
//copied[2] should be '/0' - the terminating character.
any suggestions?
tom
-
Well, a character is little more than an integer with a smaller range. The following will work:
Code:
copied[0] = 0;
copied[1] = 6;
copied[3] = 0;
Note that '\0' and 0 are the same value. This may cause problems for you with copied[0]. If you want the ASCII characters zero and six (and not the numeric values zero and six), try:
Code:
copied[0] = '0';
copied[1] = '6';
copied[3] = 0; /* same as '\0' */
-
I also have the feeling I've misunderstood your question. If you want to take numeric values and put their ASCII equivalent in a string, look into sprintf(). :)
Code:
sprintf( my_string, "The number is %d\n", my_number );
-
Don't cross post please.
-Prelude