Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert an ASCII value into a character in .NET

There are a million posts on here on how to convert a character to its ASCII value.
Well I want the complete opposite.
I have an ASCII value stored as an int and I want to display its ASCII character representation in a string.

i.e. please display the code to convert the int 65 to A.

What I have currently is String::Format("You typed '{0}'", (char)65)

but this results in "You typed '65'" whereas I want it to be "You typed 'A'"

I am using C++/CLI but I guess any .NET language would do...

(edited post-humously to improve the question for future googlers)

like image 231
demoncodemonkey Avatar asked Oct 15 '09 19:10

demoncodemonkey


People also ask

How do I convert ASCII characters?

Very simple. Just cast your char as an int . char character = 'a'; int ascii = (int) character; In your case, you need to get the specific Character from the String first and then cast it.

How do I get the ASCII value of a character in VB net?

The Chr function in VB.NET converts the integer back to the character: Dim i As Integer = Asc("x") ' Convert to ASCII integer. Dim x As Char = Chr(i) ' Convert ASCII integer to char.

What is the ASCII value of A to Z?

Below are the implementation of both methods: Using ASCII values: ASCII value of uppercase alphabets – 65 to 90. ASCII value of lowercase alphabets – 97 to 122.


2 Answers

For ASCII values, you should just be able to cast to char? (C#:)

char a = (char)65;

or as a string:

string a = ((char)65).ToString();
like image 178
Marc Gravell Avatar answered Oct 23 '22 14:10

Marc Gravell


In C++:

int main(array<System::String ^> ^args)
{
    Console::WriteLine(String::Format("You typed '{0}'", Convert::ToChar(65)));
    return 0;
}
like image 20
Austin Salonen Avatar answered Oct 23 '22 14:10

Austin Salonen