Ascii character conversion
Does anyone know of a way to assign a variable the ascii character of a given value? For example, 97 is the value of the ascii 'a'. If I have
int num = 97;
char letter;
How would I assign 'a' to letter?
I was using 'a' as an example. I need it to assign the the of num to letter whatever it's value. I'm going to assign a random number to num, then assign that character to letter.
Well, a char just holds a number, which is usually the ascii code of your character, hence the name. You could hold small numbers in it if you wanted. You're guaranteed 0-127. It might even hold more.
Write char letter = 'a'+(rand()%26); if you want a random letter. Be sure to call srand() at the start of your program, though.
Write char letter = 'a'+(rand()%26); if you want a random letter. Be sure to call srand() at the start of your program, though.
You don't understand what I want. I know how to assign the letter 'a' to a variable. I'm trying to make a random letter basically, by making a random number 97-122, then assigning a variable the ascii character of that number.
And I'm saying a character is just a number. Assign your number from 97 to 122 to letter. There's nothing special you need to do.
A char is not just a number. It can also hold letters, which is what I want it to do. You obviously don't understand ASCII, go google it before you keep repeating yourself. I don't want letter to hold a number 97-122, I want it to hold an ASCII character with a value 97-122, also known as a lowercase letter. ;-)
But the smart_idiot is completely right. :)
If you assign a number from 97 to 122 to your variable like
char letter = 97 + rnd(26);
then your variable holds the ASCII code of that character.
And that was your question, right ?
If you assign a number from 97 to 122 to your variable like
char letter = 97 + rnd(26);
then your variable holds the ASCII code of that character.
And that was your question, right ?
Quote:
Does anyone know of a way to assign a variable the ascii character of a given value?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement