# Thread: Convert integer to char..

1. ## Convert integer to char..

Hi..

I am trying to convert an integer to a char.. How can I do this..?

ex.
int i = 7;
char p[12] = "testint = i"
p[10] = i; // HOW CAN I DO THIS
p[11] = '\0'

or

int i = 7;
char *p = "testint = "
strcat(p,i) // DOES NOT SEEM TO WORK NEIGHTER

2. just cast it.
e.g.
int i = 7;
char p[12] = "testint = i"
p[10] = (char)i;
...

3. Do you mean option 1 from here? http://faq.cprogramming.com/cgi-bin/...&id=1043284385

Casting (int)7 to a character would give you ASCII character 7, which is the same as \a, I think. In other words, your computer would beep. If it's '7' you're looking for, just add '0'.
Code:
```int number = 7;
char character = number + '0';

printf("&#37;d %c\n", number, character);```
Output:
Code:
`7 7`

4. Abda92.. Does not seem to work..

PORTlastNumber = 7;
PORT = 31000 + PORTlastNumber;
//char *p="smrdemo -t 5";
//char *s;
//int j=4;
char p[13]; //="smrdemo -t ";
p[0] = 's'; p[1] = 'm'; p[2] = 'r';p[3] = 'd';p[4] = 'e';p[5] = 'm';p[6] = 'o';p[7] = ' ';p[8] = '-';p[9] = 't';p[10] = ' ';p[11] = '7';p[12] = '\0';

//"smrdemo -t 7";
//strcat(p,itoa(j,buff,10));
//sprintf(s,"smrdemo -t %i",5);
//printf("s --> %s \n",s);
//printf("p --> %s \n",p);
p[11] = (char) PORTlastNumber;//PORTlastNumber;
printf("JJJJJ->%s \n",p);
printf("FFFFF->%c \n",p[11]);
Returns:
JJJJJ->smrdemo -t
FFFFF->

5. Originally Posted by dwks
Do you mean option 1 from here? http://faq.cprogramming.com/cgi-bin/...&id=1043284385

Casting (int)7 to a character would give you ASCII character 7, which is the same as \a, I think. In other words, your computer would beep. If it's '7' you're looking for, just add '0'.
Code:
```int number = 7;
char character = number + '0';

printf("%d %c\n", number, character);```
Output:
Code:
`7 7`
Yes... PERFECT... Thank you alot.. I works just fine.. You just saved rest of my evening... Thanks alot..

6. Abda92's solution won't work, as you have seen. Try this.
Code:
`p[11] = PORTlastNumber + '0';`
Code:
```char p[13]; //="smrdemo -t ";
p[0] = 's'; p[1] = 'm'; p[2] = 'r';p[3] = 'd';  /* ... */```
Consider using
Code:
`char p[13] = "smrdemo -t 7";`
The 13 size is also optional. If you leave it out, the compiler will count how many bytes are required for the string (including the NULL). This only works if you initialize the string like I have done.

 I see it's working now. Good luck. [/edit]

7. Originally Posted by dwks
Abda92's solution won't work, as you have seen. Try this.
Code:
`p[11] = PORTlastNumber + '0';`
Code:
```char p[13]; //="smrdemo -t ";
p[0] = 's'; p[1] = 'm'; p[2] = 'r';p[3] = 'd';  /* ... */```
Consider using
Code:
`char p[13] = "smrdemo -t 7";`
The 13 size is also optional. If you leave it out, the compiler will count how many bytes are required for the string (including the NULL). This only works if you initialize the string like I have done.

 I see it's working now. Good luck. [/edit]
I know about the char p[13]="smrdemo -t ";. It was just a desperate try to fix it.. And Thanks again..

8. Sorry, I guess I understood the question wrong.