how to get/set ASCII code of a character?
-
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
-
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
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; }
-
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
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
-
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
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
-
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