+how to C# convert value to ascii ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kate77
    New Member
    • Oct 2007
    • 14

    +how to C# convert value to ascii ?

    Hi,
    Im trying to build simple encryption,
    I use
    char d="a"
    int intASC = System.Convert. ToInt32(d);

    to convert it, and play with it,
    now I want to change it (d) back for the original ascii value ("a"),
    how do I do that ?
  • Shashi Sadasivan
    Recognized Expert Top Contributor
    • Aug 2007
    • 1435

    #2
    Do you mean you want to convert it from the ascii value to the char value?


    [CODE=cpp] char x = Convert.ToChar( intASC);[/CODE]

    PS: From your code that you gave, you should recieve a compilation error.
    [CODE=cpp]char d = 'a'; //d is a char not a string[/CODE]
    cheers

    Comment

    • Kate77
      New Member
      • Oct 2007
      • 14

      #3
      Sorry if I was not clear,
      I have the following code:

      class encrypt
      {
      public string enkrypt(string enc)
      {

      char[] thechars = enc.ToCharArray ();
      string tata = "";
      foreach (char d in thechars)
      {
      int intASC = System.Convert. ToInt32(d);
      tata = intASC.ToString ();
      }

      return (tata);

      }
      }

      now, when I call it, I get the value in this case 97.
      encrypt enc = new encrypt();
      MessageBox.Show (enc.enkrypt("A "));

      what I am looking for now is a function that will do the opposite of
      int intASC = System.Convert. ToInt32(d);

      I want to take 97 and translate it to A ,
      I am sure it is simple so forgive me if it sounds like stupid question.

      Thanks,
      -Kate


      Originally posted by Shashi Sadasivan
      Do you mean you want to convert it from the ascii value to the char value?


      [CODE=cpp] char x = Convert.ToChar( intASC);[/CODE]

      PS: From your code that you gave, you should recieve a compilation error.
      [CODE=cpp]char d = 'a'; //d is a char not a string[/CODE]
      cheers

      Comment

      • Shashi Sadasivan
        Recognized Expert Top Contributor
        • Aug 2007
        • 1435

        #4
        That is going to be tough, especially with no index.
        your ascii values could be from a single digit to 3 digits long
        so the string 167 could be interpreted in a lot of different ways.

        Guess we need to come up with something better

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          Consider this:

          Code:
          char myc = 'a';
          int myint = (int)myc;
          //now myint will = 97 (the ascii value)
          myint++;//make myint=98;
          char myreturnc= (char)myint;
          //now myreturnc='b'

          Comment

          • Kate77
            New Member
            • Oct 2007
            • 14

            #6
            its working!!
            thank you,
            sorry for the questions,
            it just seems that unlike php finding how to do the simple things in C#
            is very difficult.

            -Kate

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              Hehe, but if you know java, C# can be done in your sleep.

              Comment

              Working...