- Home /
Converting a single String character to representative int. How? Javascript
Say I have the letter "A" in a string variable.
And I want to perform "addition" upon it, so that it becomes "B", then "C", and then later I want to go backwards, from "C" to "B", and "B" to "C".
I've seen somewhere that this can be represented numerically, by a number between 0 and 255, but don't know what this numerical representation of the alphabet is called. Or how to change the string to this format for addition, and then back to a string for printing.
The question, how do I turn that variable string of the letter "A" into that representative number, add 1 to that number, and then pass it back to the string variable as the new number, thereby increasing the "A" to "B".
Or is there are a better way to do this?
Answer by Molix · Feb 19, 2011 at 03:30 PM
It sounds like you want the hexadecimal equivalent of "A". Hexadecimal is base-16, and uses the digits 0-9 then A-F.
Here's an example of converting hex and back in C#.
Edit, on re-reading, I'm thinking you might just want the character code version of "A". In that case, you can just do:
string str = "A";
char c = str[0];
Debug.Log( c ); // prints A
c++;
Debug.Log( c ); // prints B
Edit, added JS:
var str : String = "A";
var c : int = str[0];
Debug.Log( System.Convert.ToChar( c ) ); // prints A
c++;
Debug.Log( System.Convert.ToChar( c ) ); // prints B
is this much the same in UnityScript (javascript)?
I can't find much on char in the unity reference that makes any sense to me.
I don't think there is a char class for UnityScript. I'm assu$$anonymous$$g that's why you have to use String.Split(","[0]) to split a string by commas
Answer by jahroy · May 16, 2011 at 05:57 PM
The integer value of a character is referred to as its ASCII value.
A is 65, B is 66, C is 67, etc...
I'm not sure if everything is the same in unicode.
var stringValue : String = "A";
/ get the int value of the first character in the string /
var intValue : int = stringValue[0];
print(intValue);
intValue ++;
var stringFromAscii : String = System.Convert.ToChar(intValue);
print(stringFromAscii);
Your answer
Follow this Question
Related Questions
Array - Convert Object into Int 5 Answers
[SOLVED] Convert this line from string to int 1 Answer
I can't print a int.ToString() variable in a guiText.text 1 Answer
GUI.Label shows volume from a slider 3 Answers
Converting a string to an int 2 Answers