how can i convert integer to char
+2
A:
A char in C is already a number (the character's ASCII code), no conversion required.
If you want to convert a digit to the corresponding character, you can simply add '0':
c = i +'0';
Ofir
2010-02-17 09:07:01
The '0' being interpreted by the compiler as representing the ASCII code of the zero character, and given that in ASCII the digits '0' to '9' fill a simple range of codes (48 to 57 IIRC).
Steve314
2010-02-17 09:20:39
A:
Just assign the int
to a char
.
int i = 65
char c = i;
printf("%c", c);//prints A
Amarghosh
2010-02-17 09:10:37
+1
A:
You can try atoi() library function. Also sscanf() and sprintf() would help.
Here is a small example to show converting integer to character string:
main()
{
int i = 247593;
char str[10];
sprintf(str, "%d", i);
// Now str contains the integer as characters
}
Here for another Example
#include <stdio.h>
int main(void)
{
char text[] = "StringX";
int digit;
for (digit = 0; digit < 10; ++digit)
{
text[6] = digit + '0';
puts(text);
}
return 0;
}
/* my output
String0
String1
String2
String3
String4
String5
String6
String7
String8
String9
*/
ratty
2010-02-17 09:14:07