Search Unity

AscII to Int - Int to AscII

Discussion in 'Scripting' started by Haegar, Jan 4, 2011.

  1. Haegar

    Haegar

    Joined:
    Jun 8, 2009
    Posts:
    58
    Hello *,

    for an application I need to get the AscII from a Char (e.g. the third char from a string) like:

    var s: string = "Hello";
    var i: int = parseInt( s[2] );

    ... but this won't work.

    Finally I'd like also to convert an integer to an AscII like:

    var s: string = (<IntToStr> 65);

    Thank you,
    Ulrich
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    System.Convert.ToInt32/ToChar. Though it's Unicode and not ASCII.

    --Eric
     
  3. Kayn

    Kayn

    Joined:
    Dec 23, 2010
    Posts:
    21
    C#

    Code (csharp):
    1. string s = "Hello";
    2. char c = s[0];
    3. int i = c;
    4.        
    5. print(i);
    Javascript
    Code (csharp):
    1. var str = "Hello";
    2. var cha = str[0];
    3. var integer:int = cha;
    4.    
    5. print(integer);
    Should both display 72 (ASCII H = 72)
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Interesting...casting from chars to ints used to not work in Unity 2.

    --Eric
     
  5. Haegar

    Haegar

    Joined:
    Jun 8, 2009
    Posts:
    58
    Thank you very much. This is what I was looking for. :)

    Ulrich
     
  6. Dheatly101

    Dheatly101

    Joined:
    Sep 11, 2014
    Posts:
    3
    But how about convert Unicode back to string?
     
  7. ToroidGames

    ToroidGames

    Joined:
    Jul 25, 2017
    Posts:
    11
    int i = 72;
    char a = Convert.ToChar(i);
    print(a);
     
    Last edited: Oct 15, 2018