How to use ASCII in C #

Source: Internet
Author: User
Net Framework uses Unicode UTF-16 to represent characters.
Use the system. Text. asciiencoding class to convert ASCII codes in. net. It provides getbytes () and getchars () for conversion. Of course, you can also find other useful functions in the. NET online manual.

The following is an example of conversion:

Using system;
Using system. text;

Namespace consoleapplication3
{

Class class1
{

Static void main (string [] ARGs)
{

// UTF-16 string
String mystring = "Hello World ";

Asciiencoding ae1 = new asciiencoding ();

Byte [] bytearray1 = ae1.getbytes (mystring );

// Print out the ASCII code
For (INT x = 0; x <= bytearray1.length-1; X ++)
{
Console. Write ("{0}", bytearray1 [x]);
}

Console. Write ("/N ");

// Convert the ASCII code to the corresponding character

Asciiencoding ae2 = new asciiencoding ();
Byte [] bytearray2 = {72,101,108,108,111, 111,114,108,100 };
Char [] chararray = ae2.getchars (bytearray2 );
For (INT x = 0; x <= chararray. Length-1; X ++)
{
Console. Write (chararray [x]);
}

Console. Write ("/N ");

}
}
}

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.