Export (0) Print
Expand All
20 out of 28 rated this helpful - Rate this topic

Encoding Class

Represents a character encoding.

For a list of all members of this type, see Encoding Members.

System.Object
   System.Text.Encoding
      System.Text.ASCIIEncoding
      System.Text.UnicodeEncoding
      System.Text.UTF7Encoding
      System.Text.UTF8Encoding

[Visual Basic]
<Serializable>
MustInherit Public Class Encoding
[C#]
[Serializable]
public abstract class Encoding
[C++]
[Serializable]
public __gc __abstract class Encoding
[JScript]
public
   Serializable
abstract class Encoding

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

Methods are provided to convert arrays and strings of Unicode characters to and from arrays of bytes encoded for a target code page.

A number of Encoding implementations are provided in the System.Text namespace, including:

The ASCIIEncoding class encodes Unicode characters as single 7-bit ASCII characters. This encoding only supports character values between U+0000 and U+007F.

The UnicodeEncoding class encodes each Unicode character as two consecutive bytes. Both little-endian (code page 1200) and big-endian (code page 1201) byte orders are supported.

The UTF7Encoding class encodes Unicode characters using the UTF-7 encoding (UTF-7 stands for UCS Transformation Format, 7-bit form). This encoding supports all Unicode character values, and can also be accessed as code page 65000.

The UTF8Encoding class encodes Unicode characters using the UTF-8 encoding (UTF-8 stands for UCS Transformation Format, 8-bit form). This encoding supports all Unicode character values, and can also be accessed as code page 65001.

Use the GetEncoding method with a code page or name parameter to obtain other encodings.

When the data to be converted is only available in sequential blocks (such as data read from a stream), an application can use a Decoder or an Encoder to perform the conversion. This is also useful when the amount of data is so large that it needs to be divided into smaller blocks. Decoders and encoders are obtained using the GetDecoder and GetEncoder methods. An application can use the properties of this class such as ASCII, Default, Unicode, UTF7, and UTF8 to obtain encodings. Applications can initialize new instances of Encoding objects through the ASCIIEncoding, UnicodeEncoding, UTF7Encoding, and UTF8Encoding classes.

Through an encoding, the GetBytes method is used to convert arrays of Unicode characters to arrays of bytes, and the GetChars method is used to convert arrays of bytes to arrays of Unicode characters. The GetBytes and GetChars methods maintain no state between conversions.

The core GetBytes and GetChars methods require the caller to provide the destination buffer and ensure that the buffer is large enough to hold the entire result of the conversion. An application can use one of the following methods to calculate the required size of the destination buffer.

  1. The GetByteCount and GetCharCount methods can be used to compute the exact size of the result of a particular conversion, and an appropriately sized buffer for that conversion can then be allocated.
  2. The GetMaxByteCount and GetMaxCharCount methods can be used to compute the maximum possible size of a conversion of a given number of bytes or characters, and a buffer of that size can then be reused for multiple conversions.

The first method generally uses less memory, whereas the second method generally executes faster.

Example

[Visual Basic] 
Imports System
Imports System.Text
Imports Microsoft.VisualBasic

Namespace Convert_Example
    Class MyConvertExampleClass
        Shared Sub Main()
            Dim unicodeString As String = "This string contains the unicode character Pi(" & ChrW(&H03A0) & ")"

            ' Create two different encodings.
            Dim ascii As Encoding = Encoding.ASCII
            Dim [unicode] As Encoding = Encoding.Unicode

            ' Convert the string into a byte[].
            Dim unicodeBytes As Byte() = [unicode].GetBytes(unicodeString)

            ' Perform the conversion from one encoding to the other.
            Dim asciiBytes As Byte() = Encoding.Convert([unicode], ascii, unicodeBytes)

            ' Convert the new byte[] into a char[] and then into a string.
            ' This is a slightly different approach to converting to illustrate
            ' the use of GetCharCount/GetChars.
            Dim asciiChars(ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)) As Char
            ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0)
            Dim asciiString As New String(asciiChars)

            ' Display the strings created before and after the conversion.
            Console.WriteLine("Original string: {0}", unicodeString)
            Console.WriteLine("Ascii converted string: {0}", asciiString)
        End Sub
    End Class
End Namespace

[C#] 
using System;
using System.Text;

namespace ConvertExample
{
   class ConvertExampleClass
   {
      static void Main()
      {
         string unicodeString = "This string contains the unicode character Pi(\u03a0)";

         // Create two different encodings.
         Encoding ascii = Encoding.ASCII;
         Encoding unicode = Encoding.Unicode;

         // Convert the string into a byte[].
         byte[] unicodeBytes = unicode.GetBytes(unicodeString);

         // Perform the conversion from one encoding to the other.
         byte[] asciiBytes = Encoding.Convert(unicode, ascii, unicodeBytes);
            
         // Convert the new byte[] into a char[] and then into a string.
         // This is a slightly different approach to converting to illustrate
         // the use of GetCharCount/GetChars.
         char[] asciiChars = new char[ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
         ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
         string asciiString = new string(asciiChars);

         // Display the strings created before and after the conversion.
         Console.WriteLine("Original string: {0}", unicodeString);
         Console.WriteLine("Ascii converted string: {0}", asciiString);
      }
   }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Text;

int main()
{
   String* unicodeString = S"This string contains the unicode character Pi(\u03a0)";

   // Create two different encodings.
   Encoding * ascii = Encoding::ASCII;
   Encoding * unicode = Encoding::Unicode;

   // Convert the string into a Byte->Item[].
   Byte unicodeBytes[] = unicode -> GetBytes(unicodeString);

   // Perform the conversion from one encoding to the other.
   Byte asciiBytes[] = Encoding::Convert(unicode, ascii, unicodeBytes);

   // Convert the new Byte into[] a char and[] then into a string.
   // This is a slightly different approach to converting to illustrate
   // the use of GetCharCount/GetChars.
   Char asciiChars[] = new Char[ascii -> GetCharCount(asciiBytes, 0, asciiBytes -> Length)];
   ascii -> GetChars(asciiBytes, 0, asciiBytes->Length, asciiChars, 0);
   String* asciiString = new String(asciiChars);

   // Display the strings created before and after the conversion.
   Console::WriteLine(S"Original String*: {0}", unicodeString);
   Console::WriteLine(S"Ascii converted String*: {0}", asciiString);
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Text

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: Mscorlib (in Mscorlib.dll)

See Also

Encoding Members | System.Text Namespace

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.