Mono Class Library: System.Text NamespaceSystem.Text.Encoding Class |
See Also: Encoding Members
|
This type is safe for multithreaded operations.
Characters are abstract entities that can be represented using many different character schemes or codepages. For example, Unicode UTF-16 encoding represents, or encodes, characters as sequences of 16-bit integers while Unicode UTF-8 represents the same characters as sequences of 8-bit bytes.
The BCL includes the following types derived from System.Text.Encoding:
- System.Text.ASCIIEncoding - encodes Unicode characters as 7-bit ASCII characters. This encoding only supports code points between U+0000 and U+007F inclusive.
- System.Text.UnicodeEncoding - encodes each Unicode character as two consecutive bytes. Both little-endian and big-endian byte orders are supported.
- System.Text.UTF8Encoding - encodes Unicode characters using the UTF-8 (UCS Transformation Format, 8-bit form) encoding. This encoding supports all Unicode character values.
An application can use the properties of this class such as Encoding.ASCII, Encoding.Default, Encoding.Unicode, and Encoding.UTF8 to obtain encodings. Applications can initialize new instances of System.Text.Encoding objects through the System.Text.ASCIIEncoding, System.Text.UnicodeEncoding, and System.Text.UTF8Encoding classes.
Through an encoding, the Encoding.GetBytes(Char[]) method is used to convert arrays of Unicode characters to arrays of bytes, and the Encoding.GetChars(Byte[]) method is used to convert arrays of bytes to arrays of Unicode characters. The Encoding.GetBytes(Char[]) and Encoding.GetChars(Byte[]) methods maintain no state between conversions. When the data to be converted is only available in sequential blocks (such as data read from a stream) or when the amount of data is so large that it needs to be divided into smaller blocks, an application can choose to use a System.Text.Decoder or a System.Text.Encoder to perform the conversion. Decoders and encoders allow sequential blocks of data to be converted and they maintain the state required to support conversions of data that spans adjacent blocks. Decoders and encoders are obtained using the Encoding.GetDecoder and Encoding.GetEncoder methods.
The core Encoding.GetBytes(Char[]) and Encoding.GetChars(Byte[]) 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. When using these methods, either directly on a System.Text.Encoding object or on an associated System.Text.Decoder or System.Text.Encoder, an application can use one of two methods to allocate destination buffers.
- The Encoding.GetByteCount(Char[]) and Encoding.GetCharCount(Byte[]) 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.
- The Encoding.GetMaxByteCount(int) and Encoding.GetMaxCharCount(int) methods can be used to compute the maximum possible size of a conversion of a given number of characters or bytes, regardless of the actual character or byte values, 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.
[Edit]
Namespace: System.Text
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0