Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: MurrayL on Sun 28/08/2011 22:27:01

Title: [SOLVED] ASCII KeyCode to Char?
Post by: MurrayL on Sun 28/08/2011 22:27:01
Is there a quick way to convert an ASCII KeyCode to a Char?

i.e.


keycodeToChar(65); //returns "A"


If not, what would be the easiest way of doing this?
I only really need to be able to do it for the alphabetical keys (65-90) if that makes things simpler.

This is for the purpose of generating random letters, so if there's another way of doing this please do let me know!
Title: Re: ASCII KeyCode to Char?
Post by: Calin Leafshade on Sun 28/08/2011 22:40:30
i think the conversion is implicit.


char c = Random(25) + 65;
Display("%c",c);


edit:

So your function could be:


String CharToString(char c){
   return String.Format("%c", c);
}
Title: Re: ASCII KeyCode to Char?
Post by: monkey0506 on Mon 29/08/2011 06:09:08
It's also worth noting that the AGS char type is an unsigned 8-bit type (1 byte). What that means is that:

char c = (255 + 5);

Is the same as:

char c = 4;

It automatically bounds values to the valid range. All of AGS' numeric types do this, but the range is so high that it's typically not even noticeable.

There's an implicit conversion in AGS between any of the integral types (bool, char, short, int, or any enumerated type). Enumerated type variables are actually allowed in AGS to have invalid values as a result of this implicit conversion, but again, that's not likely to cause issues.

So, any of these methods of setting a char are valid:

char A = 65;
char B = 'B';
char C = eKeycodeC;
char D = A + 3;
Title: Re: ASCII KeyCode to Char?
Post by: MurrayL on Mon 29/08/2011 12:18:59
Thanks guys; just what I needed! :)