Stringascii conversion (from msdn)

Source: Internet
Author: User

. NET Framework developer Guide

For basic type encoding, see
System. Text | system. Text. asciiencoding | system. Text. Encoding | system. Text. unicodeencoding | system. Text. utf7encoding | system. Text. utf8encoding | use the base type
Language
C #

Visual Basic

Show all
A character is an abstract entity that can be expressed using multiple character schemes or code pages. For example, Unicode UTF-16 encoding represents a 16-bit integer sequence, while Unicode UTF-8 encoding represents the same character as an 8-bit sequence. The Common Language Runtime uses Unicode UTF-16 (UNICODE conversion format, 16-bit encoding form) to represent characters.

Applications targeting the Common Language Runtime Library use encoding to map the character table format from the local character scheme to other schemes. The application uses decoding to map characters from a non-local program to a local program. The following table lists the most common classes used for character encoding and decoding in the system. Text namespace.

Description of the character Scheme
The ASCII code system. Text. asciiencoding converts the characters into ASCII characters.
Multiple encodings: system. Text. Encoding converts the characters to the various encodings specified in the convert method.
UTF-16 unicode encoding system. Text. unicodeencoding converts between other encodings and UTF-16 encodings. In this scenario, the characters are expressed as 16-digit integers.
UTF-8 unicode encoding system. Text. utf8encoding converts between other encodings and UTF-8 encodings. This variable-width encoding scheme uses one to four bytes to represent characters.

The following code uses the asciiencoding. getbytes method to convert a unicode string to a byte array. Each byte in the array represents the ASCII Value of the letter at this position in the string.

[Visual Basic]
Dim mystring as string = "encoding string ."
Dim AE as new asciiencoding ()
Dim bytearray as byte () = AE. getbytes (mystring)
Dim X as integer
For x = 0 to bytearray. Length-1
Console. Write ("{0}", bytearray (x ))
Next

[C #]
String mystring = "encoding string .";
Asciiencoding AE = new asciiencoding ();
Byte [] bytearray = AE. getbytes (mystring );
For (INT x = 0; x <= bytearray. Length-1; X ++)
{
Console. Write ("{0}", bytearray [x]);
}

In this example, the following content is displayed on the console. Byte 69 is the ASCII value of character E, byte 110 is the ascii value of character n, and so on.

69 110 99 111 100 105 110 103 32 83 116 114 105 110 46

The following code example uses the asciiencoding class to convert the byte array to a character array. The getchars method is used to decode the byte array.

[Visual Basic]
Dim AE as new asciiencoding ()
Dim bytearray as byte () = {69,110, 99,111,100,105,110,103, 32, 83,116,114,105,110,103, 46}
Dim chararray as char () = AE. getchars (bytearray)
Dim X as integer
For x = 0 to chararray. Length-1
Console. Write (chararray (x ))
Next

[C #]
Asciiencoding AE = new asciiencoding ();
Byte [] bytearray = {69,110, 99,111,100,105,110,103, 32, 83,116,114,105,110,103, 46 };
Char [] chararray = AE. getchars (bytearray );
For (INT x = 0; x <= chararray. Length-1; X ++)
{
Console. Write (chararray [x]);
}

The above code displays the encoding string. Text on the console.

See
System. Text | system. Text. asciiencoding | system. Text. Encoding | system. Text. unicodeencoding | system. Text. utf7encoding | system. Text. utf8encoding | use the base type

Send comments on this topic

? 2001-2002 Microsoft Corporation. All rights reserved.

 

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.