Home >Backend Development >C++ >How can I convert an integer to an ASCII character in C ?
To convert an integer to an ASCII character, several methods are available:
Straightforward:
char digits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; char aChar = digits[i];
Safe:
char aChar = '0' + i;
Generic:
itoa(i, ...)
Handy:
sprintf(myString, "%d", i)
C :
std::ostringstream oss; oss << 6;
Humorous:
char aChar = '6'; //int i = 6;
To generate a random number, convert it to a character, add a ".txt" suffix, and access the file in an ifstream:
// Generate random number srand(time(NULL)); int randomInt = rand() % 10; // Convert to character char randomChar = '0' + randomInt; // Add ".txt" and open file std::string filename = randomChar + ".txt"; std::ifstream file(filename.c_str());
The above is the detailed content of How can I convert an integer to an ASCII character in C ?. For more information, please follow other related articles on the PHP Chinese website!