[SOLVED] ASCII KeyCode to Char?

Started by MurrayL, Sun 28/08/2011 22:27:01

Previous topic - Next topic

MurrayL

Is there a quick way to convert an ASCII KeyCode to a Char?

i.e.

Code: ags

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!

Calin Leafshade

#1
i think the conversion is implicit.

Code: ags

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


edit:

So your function could be:

Code: ags

String CharToString(char c){
   return String.Format("%c", c);
}

monkey0506

It's also worth noting that the AGS char type is an unsigned 8-bit type (1 byte). What that means is that:

Code: ags
char c = (255 + 5);


Is the same as:

Code: ags
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:

Code: ags
char A = 65;
char B = 'B';
char C = eKeycodeC;
char D = A + 3;

MurrayL

Thanks guys; just what I needed! :)

SMF spam blocked by CleanTalk