C # Learning Experience of character ASCII codes

Source: Internet
Author: User

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.

  1. Private void testchar (){

  2. Char CH = 'a'; short II = 65;

  3. This. textbox1.text = "";

  4. This. textbox1.appendtext ("the ASCII code \'"
    + CH + "\ 'is:" + (short) CH + "\ n ");

  5. This. textbox1.appendtext ("ASCII is" + II. tostring () +
    ", The char is:" + (char) II + "\ n ");

  6. Char Cn = '中'; short UC = 22478;

  7. This. textbox1.appendtext ("the Unicode of \ '" + CN + "\' is:
    "+ (Short) CN +" \ n ");

  8. This. textbox1.appendtext ("Unicode is" + UC. tostring () +
    ", The char is:" + (char) UC + "\ n ");

  9. }

Its running result is

  1. The ASCII code of 'A' is: 97

  2. ASCII is 65, the char is:

  3. The Unicode of 'is: 20013

  4. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.