Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: zabnat on Thu 31/08/2006 08:39:42

Title: Converting char -> int
Post by: zabnat on Thu 31/08/2006 08:39:42
How can I convert char to int so that A would become 64 etc...
Or is it even possible?

I'm thinking of reading numerical data from a file and 8-bit values would be enough for me and it would create unneccasary overhead in the file if I would use 32-bit values there. So I'm thinking I would read them in as char array an use them from there with conversion.

edit:
Or maybe I could read them in groups of fours and then separate them with bitwise operators, but script language doesn't have those?
Title: Re: Converting char -> int
Post by: Gilbert on Thu 31/08/2006 08:47:13
If it's a variable of char type, it's compatible with integer arimetic already.

For example:

char BLAH = 'A'; //SAME as doing char BLAH = 64;
int ttt;

ttt = BLAH * 2;
Display ("%c, %d, %d", BLAH, BLAH, ttt); //should print out "A, 64, 128"

Note single quoted character (like 'A') means char value (which is actually a 1-byte integer) while double quoted ones (like "A") is a string/String, which is different.
To get the say, 4th character of a String into the char type, you may do something like:
String HA = "MWAHAHA!";
char BLAH = HA.Chars[3]; //index starts at 0
Title: Re: Converting char -> int
Post by: zabnat on Thu 31/08/2006 08:51:41
Ok, I'll try that soon and see how it turns out.
Thanks
Title: Re: Converting char -> int
Post by: Khris on Thu 31/08/2006 11:36:42
Quote from: zabnat on Thu 31/08/2006 08:39:42Or maybe I could read them in groups of fours and then separate them with bitwise operators, but script language doesn't have those?

AGS supports >>, <<, &, | and ^.
http://www.adventuregamestudio.co.uk/manual/Operators.htm
Title: Re: Converting char -> int
Post by: SSH on Thu 31/08/2006 11:42:13
Don't put character 0 into a String, though, as it will mess it up.
Title: Re: Converting char -> int
Post by: monkey0506 on Fri 01/09/2006 01:11:33
Errmmm...I guess I'm being kind of nit-picky with this...but...A is 65, not 64...

[EDIT:]

ASCII keycode table (http://www.adventuregamestudio.co.uk/manual/ASCIIcodes.htm)