Most of the time, we need to get an ascii code of an English character, a unicode code of a Chinese character, or query the encoding of a character from the relevant encoding. Many people, especially those who learn C # From the VB program sequence, complain about why C # does not provide ready-made functions to do this-because there is ASC () in VB () functions and CHR () functions are used for this type of conversion.
However, if you have learned C, you will know that we only need to forcibly convert the English numeric data into appropriate numeric data to get the corresponding C # ASCII code. Otherwise, if you forcibly convert a suitable numeric data to numeric data, you can get the corresponding characters.
In C #, the character range is extended, including not only single-byte characters, but also double-byte characters, such as Chinese characters. The conversion between characters and encoding is still extended by the C language-forced conversion. Let's take a look at the example below.
Private void testchar (){
Char CH = 'a'; short II = 65;
This. textbox1.text = "";
This. textbox1.appendtext ("the ASCII code \'"
+ CH + "\ 'is:" + (short) CH + "\ n ");
This. textbox1.appendtext ("ASCII is" + II. tostring () +
", The char is:" + (char) II + "\ n ");
Char Cn = '中'; short UC = 22478;
This. textbox1.appendtext ("the Unicode of \ '" + CN + "\' is:
"+ (Short) CN +" \ n ");
This. textbox1.appendtext ("Unicode is" + UC. tostring () +
", The char is:" + (char) UC + "\ n ");
}
Its running result is
The ASCII code of 'A' is: 97
ASCII is 65, the char is:
The Unicode of 'is: 20013
Unicode is 22478, the char is: City
In this example, we can have a very clear understanding-through forced conversion, we can be able to encode or get the characters represented by encoding. If you do not need short encoding, you can get int and other types of encoding values.