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
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
just cast it.
e.g.
int i = 7;
char p[12] = "testint = i"
p[10] = (char)i;
...
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'.
Output:Code:int number = 7; char character = number + '0'; printf("%d %c\n", number, character);
Code:7 7
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Abda92.. Does not seem to work..
Returns: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]);
JJJJJ->smrdemo -t
FFFFF->
Abda92's solution won't work, as you have seen. Try this.
Also, instead of this:Code:p[11] = PORTlastNumber + '0';
Consider usingCode:char p[13]; //="smrdemo -t "; p[0] = 's'; p[1] = 'm'; p[2] = 'r';p[3] = 'd'; /* ... */
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.Code:char p[13] = "smrdemo -t 7";
[edit] I see it's working now. Good luck. [/edit]
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Sorry, I guess I understood the question wrong.