Hi, stupid newbie
is there an equivalent function that will return the ascii value of a character, in the same was as C/C++ atoi?
I’m finding Unities javascript string handling a bit frustrating, I realize its derived from mono and not javascript, but a page in the script reference detailing the string functions would be a great help.
In C, Atoi returned the numerical value of a string, not the ascii code. The equivalent of atoi would be string.ParseInt();
To return the ascii value of a key would be the charCodeAt() method.
your right atoi is the same as ParseInt();
however what I want to do is walk a string, and take the ascii value of each letter which I then want to process as an int
eg
var str=“hellow world”;
for (i=0;i<str.length;i++)
DoStuff((int)str*);*
by the way are
- parseInt*
and
- toString*
supported in unity, I’m getting an error?
Seems two steps are required to perform the cast
first import the mono system libs,then use the convert function
import System;
var ascii = Convert.ToInt32("a"[0]);
Just to note, “import” only imports a namespace to make it easier to type stuff and doesn’t really import any libraries on its own. You can do it in one line like so:
var ascii = System.Convert.ToInt32("a"[0]);
Of course then you have to use “System.Convert” all the time; if you do the import, then all you have to use is “Convert”. This works with everything…for example, if you do “import Debug;” then you can type “Log” instead of “Debug.Log”. The drawback being that your code becomes less portable, since you need that import line or else it won’t work, so you have to keep that in mind when copying pasting into other scripts.
–Eric