How To Convert A Number To an ASCII Character? How To Convert A Number To an ASCII Character? windows windows

How To Convert A Number To an ASCII Character?


You can use one of these methods to convert number to an ASCII / Unicode / UTF-16 character:

You can use these methods convert the value of the specified 32-bit signed integer to its Unicode character:

char c = (char)65;char c = Convert.ToChar(65); 

Also, ASCII.GetString decodes a range of bytes from a byte array into a string:

string s = Encoding.ASCII.GetString(new byte[]{ 65 });

Keep in mind that, ASCIIEncoding does not provide error detection. Any byte greater than hexadecimal 0x7F is decoded as the Unicode question mark ("?").


Edit: By request, I added a check to make sure the value entered was within the ASCII range of 0 to 127. Whether you want to limit this is up to you. In C# (and I believe .NET in general), chars are represented using UTF-16, so any valid UTF-16 character value could be cast into it. However, it is possible a system does not know what every Unicode character should look like so it may show up incorrectly.

// Read a line of inputstring input = Console.ReadLine();int value;// Try to parse the input into an Int32if (Int32.TryParse(input, out value)) {    // Parse was successful    if (value >= 0 and value < 128) {        //value entered was within the valid ASCII range        //cast value to a char and print it        char c = (char)value;        Console.WriteLine(c);    }}


To get ascii to a number, you would just cast your char value into an integer.

char ascii = 'a'int value = (int)ascii

Variable value will now have 97 which corresponds to the value of that ascii character

(Use this link for reference)http://www.asciitable.com/index/asciifull.gif