Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. how to get/set ASCII code of a character?

how to get/set ASCII code of a character?

Scheduled Pinned Locked Moved C#
tutorialquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    largs
    wrote on last edited by
    #1

    How could i get (and then set) ASCII code of a character?For example when i pass a character A i want to get the ASCII code of it.If i change the ASCII code of a character does its function will change?I change the ASCII code of character A to B.Does character A change to B?Thanks for reading

    G L R 3 Replies Last reply
    0
    • L largs

      How could i get (and then set) ASCII code of a character?For example when i pass a character A i want to get the ASCII code of it.If i change the ASCII code of a character does its function will change?I change the ASCII code of character A to B.Does character A change to B?Thanks for reading

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      Do you really mean ASCII character codes? The Char data type in .NET is a Unicode character, so it doesn't use ASCII characters codes. If you want to use ASCII character codes you would have to encode the string into ASCII, and that leaves you with an array of bytes that you can't use as text unless you decode it again. Assuming that you mean Unicode character codes, and not ASCII character codes: The Char data type is a 16 bit value that represeents a Unicode character. You can easily convert between Char and Int32, and manipulate the value in either form:

      char c = 'A';
      c++; // c contain 'B'
      c += '!'; // c contain 'c' (66+33=97)
      int i = (int)c; // x contain 97
      i++; // x contain 98
      char d = (char)i; // d contains 'd'

      --- b { font-weight: normal; }

      1 Reply Last reply
      0
      • L largs

        How could i get (and then set) ASCII code of a character?For example when i pass a character A i want to get the ASCII code of it.If i change the ASCII code of a character does its function will change?I change the ASCII code of character A to B.Does character A change to B?Thanks for reading

        L Offline
        L Offline
        Le centriste
        wrote on last edited by
        #3

        This code does that:

        using System;
        using System.Collections.Generic;
        using System.Text;

        namespace ConsoleApplication2
        {
        class Program
        {
        static void Main(string[] args)
        {
        char c = 'A';
        int i = Convert.ToInt32(c);

                Console.WriteLine(c);
                Console.WriteLine(i);
        
                ++i;
                c = Convert.ToChar(i);
        
                Console.WriteLine(c);
                Console.WriteLine(i);
            }
        }
        

        }

        Hope this helps. -------- "I say no to drugs, but they don't listen." - Marilyn Manson

        1 Reply Last reply
        0
        • L largs

          How could i get (and then set) ASCII code of a character?For example when i pass a character A i want to get the ASCII code of it.If i change the ASCII code of a character does its function will change?I change the ASCII code of character A to B.Does character A change to B?Thanks for reading

          R Offline
          R Offline
          Rizwan Majeed
          wrote on last edited by
          #4

          You can get/set ACSII code for any of the character and vice versa as follows: // The encoding. ASCIIEncoding ascii = new ASCIIEncoding(); //Conversion From ACSII code to character Byte[] decodedBytesForChar= ascii.GetBytes("A"); //returns ACSII code for character A i.e. 65 Console.WriteLine("ACSII code for character A = {0}", decodedBytesForChar[0]); //Conversion From ACSII character to code Byte[] encodedBytes = new Byte[1]; encodedBytes[0] = 97; Console.WriteLine("ACSII character for code 97 = {0}", ascii.GetString(encodedBytes)); Rizwan

          L 1 Reply Last reply
          0
          • R Rizwan Majeed

            You can get/set ACSII code for any of the character and vice versa as follows: // The encoding. ASCIIEncoding ascii = new ASCIIEncoding(); //Conversion From ACSII code to character Byte[] decodedBytesForChar= ascii.GetBytes("A"); //returns ACSII code for character A i.e. 65 Console.WriteLine("ACSII code for character A = {0}", decodedBytesForChar[0]); //Conversion From ACSII character to code Byte[] encodedBytes = new Byte[1]; encodedBytes[0] = 97; Console.WriteLine("ACSII character for code 97 = {0}", ascii.GetString(encodedBytes)); Rizwan

            L Offline
            L Offline
            largs
            wrote on last edited by
            #5

            thanks for all replies.I want to reassign the function of the keyboard.For example when i press A on the keyboard it will print B or something else on the screen.This what i mean "set" (reassign)ASCII code.Can anyone help me?Thanks again

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups