I have
int i = 6;
and I want
char c = '6'
by conversion. Any simple way to suggest?
EDIT: also i need to generate a random number, and convert to a char, then add a '.txt' and access it in an ifstream.
You can use one of these methods to convert number to an ASCII / Unicode / UTF-16 character: You can use these methods convert the value of the specified 32-bit signed integer to its Unicode character: char c = (char)65; char c = Convert. ToChar(65);
Use the chr() Function to Convert int to ASCII in Python The chr() function is one of them. The chr() function is available to use in Python 3 and above and is utilized to provide the ASCII value of a corresponding ASCII code number.
We can convert int to char in java using typecasting. To convert higher data type into lower, we need to perform typecasting. Here, the ASCII character of integer value will be stored in the char variable. To get the actual value in char variable, you can add '0' with int variable.
In C programming, a character variable holds ASCII value (an integer number between 0 and 127) rather than that character itself. This integer value is the ASCII code of the character. For example, the ASCII value of 'A' is 65.
Straightforward way:
char digits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; char aChar = digits[i];
Safer way:
char aChar = '0' + i;
Generic way:
itoa(i, ...)
Handy way:
sprintf(myString, "%d", i)
C++ way: (taken from Dave18 answer)
std::ostringstream oss; oss << 6;
Boss way:
Joe, write me an int to char converter
Studboss way:
char aChar = '6';
Joe's way:
char aChar = '6'; //int i = 6;
Nasa's way:
//Waiting for reply from satellite...
Alien's way: '9'
//Greetings.
God's way:
Bruh I built this
Peter Pan's way:
char aChar; switch (i) { case 0: aChar = '0'; break; case 1: aChar = '1'; break; case 2: aChar = '2'; break; case 3: aChar = '3'; break; case 4: aChar = '4'; break; case 5: aChar = '5'; break; case 6: aChar = '6'; break; case 7: aChar = '7'; break; case 8: aChar = '8'; break; case 9: aChar = '9'; break; default: aChar = '?'; break; }
Santa Claus's way:
//Wait till Christmas! sleep(457347347);
Gravity's way:
//What
'6' (Jersey) Mikes'™ way:
//
SO way:
Guys, how do I avoid reading beginner's guide to C++?
My way:
or the highway.
Comment: I've added Handy way and C++ way (to have a complete collection) and I'm saving this as a wiki.
Edit: satisfied?
This will only work for int-digits 0-9, but your question seems to suggest that might be enough.
It works by adding the ASCII value of char '0'
to the integer digit.
int i=6; char c = '0'+i; // now c is '6'
For example:
'0'+0 = '0' '0'+1 = '1' '0'+2 = '2' '0'+3 = '3'
Edit
It is unclear what you mean, "work for alphabets"? If you want the 5th letter of the alphabet:
int i=5; char c = 'A'-1 + i; // c is now 'E', the 5th letter.
Note that because in C/Ascii, A is considered the 0th letter of the alphabet, I do a minus-1 to compensate for the normally understood meaning of 5th letter.
Adjust as appropriate for your specific situation.
(and test-test-test! any code you write)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With