다음을 통해 공유


Encoding Base Types

Characters are abstract entities that can be represented using many different character schemes or code pages. For example, Unicode UTF-16 encoding represents characters as sequences of 16-bit integers, whereas Unicode UTF-8 represents the same characters as sequences of 8-bit bytes. The common language runtime uses Unicode UTF-16 (Unicode Transformation Format, 16-bit encoding form) to represent characters.

Applications that target the common language runtime use encoding to map character representations from the native character scheme to other schemes. Applications use decoding to map characters from non-native schemes to the native scheme. The following table lists the most commonly used classes in the System.Text namespace to encode and decode characters.

Character Scheme Class Explanation
ASCII encoding System.Text.ASCIIEncoding Converts to and from ASCII characters.
Multiple encoding System.Text.Encoding Converts characters to and from various encodings as specified in the Convert method.
UTF-16 Unicode encoding System.Text.UnicodeEncoding Converts to and from UTF-16 encoding. This scheme represents characters as 16-bit integers.
UTF-8 Unicode encoding System.Text.UTF8Encoding Converts to and from UTF-8 encoding. This variable-width encoding scheme represents characters using one to four bytes.

The following code example converts a Unicode string into an array of bytes using the ASCIIEncoding.GetBytes method. Each byte in the array represents the ASCII value for the letter in that position of the string.

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]);
}

This example displays the following to the console. The byte 69 is the ASCII value for the E character; the byte 110 is the ASCII value for the n character, and so on.

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

The following code example converts the preceding array of bytes into an array of characters using the ASCIIEncoding class. The GetChars method is used to decode the array of bytes.

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 preceding code displays the text Encoding String. to the console.

See Also

System.Text | System.Text.ASCIIEncoding | System.Text.Encoding | System.Text.UnicodeEncoding | System.Text.UTF7Encoding | System.Text.UTF8Encoding | Working With Base Types