System.Text.Encoding
4.0.0.0
4.0.10.0
mscorlib
1.0.5000.0
2.0.0.0
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
System.Runtime
4.2.0.0
4.2.1.0
4.2.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
System.Object
[System.Serializable]
[<System.Serializable>]
[System.Runtime.InteropServices.ComVisible(true)]
[<System.Runtime.InteropServices.ComVisible(true)>]
Converts a sequence of encoded bytes into a set of characters.
class, call the method of an implementation.
The method determines how many characters result in decoding a sequence of bytes, and the method performs the actual decoding. There are several versions of both of these methods available in the class. For more information, see . A object maintains state information between successive calls to `GetChars` or methods so it can correctly decode byte sequences that span blocks. The also preserves trailing bytes at the end of data blocks and uses the trailing bytes in the next decoding operation. Therefore, and are useful for network transmission and file operations because those operations often deal with blocks of data instead of a complete data stream.
> [!NOTE]
> When the application is done with a stream of data, it should make sure that the state information is flushed by setting the `flush` parameter to `true` in the appropriate method call. If an exception occurs or if the application switches streams, it should call to clear the internal state of the `Decoder` object.
]]>
to convert two different byte arrays into a character array. One of the character's bytes spans the arrays. This is similar to what a object does internally when reading a stream.
:::code language="csharp" source="~/snippets/csharp/System.Text/Decoder/Overview/source.cs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/System.Text/Decoder/Overview/source.vb" id="Snippet1":::
]]>
When your application inherits from this class, it must override all the members.
Understanding Encodings
Constructor
System.Text.Encoding
4.0.0.0
4.0.10.0
mscorlib
1.0.5000.0
2.0.0.0
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
System.Runtime
4.2.0.0
4.2.1.0
4.2.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
Initializes a new instance of the class.
method of a implementation.
## Examples
The following example demonstrates two techniques for initializing a new instance.
:::code language="csharp" source="~/snippets/csharp/System.Text/Decoder/.ctor/ctor.cs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/System.Text/Decoder/.ctor/ctor.vb" id="Snippet1":::
]]>
mscorlib
2.0.0.0
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
System.Runtime
4.2.0.0
4.2.1.0
System.Text.Encoding
Converts an encoded byte sequence to a string or array of characters.
object saves state between calls to .
When the application is done with a stream of data, it should set the `flush` parameter to `true` to make sure that the state information is flushed. With this setting, the decoder ignores invalid bytes at the end of the data block and clears the internal buffer.
Any remaining processed data that is part of a logical unit, such as the high surrogate of a surrogate pair, is converted according to the current fallback settings.
The `Convert` method is designed to be used in a loop to decode an arbitrary amount of input, such as data read from a file or stream.
It stores the output of the decoding operation in a fixed-size buffer.
will throw an exception if the output buffer isn't large enough, but will fill as much space as possible and return the bytes read and chars written, provided the output array allows for at least two characters.
Also see for more comments.
]]>
Method
System.Runtime
4.2.1.0
4.2.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
mscorlib
netstandard
2.1.0.0
System.Text.Encoding
System.Void
A read-only bytes span containing the sequence to convert.
The span to store the converted characters.
to indicate no further data is to be converted; otherwise, .
When this method returns, contains the number of bytes that were produced by the conversion. This parameter is passed uninitialized.
When this method returns, contains the number of characters from that were used in the conversion. This parameter is passed uninitialized.
When this method returns, contains if all the specified characters were converted; otherwise, . This parameter is passed uninitialized.
Converts a span of encoded bytes to UTF-16 encoded characters and stores the result in another span buffer.
object that has not been stored in the `bytes` span.
]]>
Method
mscorlib
2.0.0.0
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
System.Runtime
4.2.0.0
4.2.1.0
4.2.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
System.Text.Encoding
[System.CLSCompliant(false)]
[<System.CLSCompliant(false)>]
[System.Runtime.InteropServices.ComVisible(false)]
[<System.Runtime.InteropServices.ComVisible(false)>]
[System.Security.SecurityCritical]
[<System.Security.SecurityCritical>]
System.Void
The address of a buffer that contains the byte sequences to convert.
The number of bytes in to convert.
The address of a buffer to store the converted characters.
The maximum number of characters in to use in the conversion.
to indicate no further data is to be converted; otherwise, .
When this method returns, contains the number of bytes that were produced by the conversion. This parameter is passed uninitialized.
When this method returns, contains the number of characters from that were used in the conversion. This parameter is passed uninitialized.
When this method returns, contains if all the characters specified by were converted; otherwise, . This parameter is passed uninitialized.
Converts a buffer of encoded bytes to UTF-16 encoded characters and stores the result in another buffer.
object that has not been stored in the `bytes` buffer.
]]>
or is ().
or is less than zero.
The output buffer is too small to contain any of the converted input. The output buffer should be at least 2 chars in size to accomodate at least one surrogate character pair.
A fallback occurred (for more information, see Character Encoding in .NET)
-and-
is set to .
Method
System.Text.Encoding
4.0.0.0
4.0.10.0
mscorlib
2.0.0.0
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
System.Runtime
4.2.0.0
4.2.1.0
4.2.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
[System.Runtime.InteropServices.ComVisible(false)]
[<System.Runtime.InteropServices.ComVisible(false)>]
System.Void
A byte array to convert.
The first element of to convert.
The number of elements of to convert.
An array to store the converted characters.
The first element of in which data is stored.
The maximum number of elements of to use in the conversion.
to indicate that no further data is to be converted; otherwise, .
When this method returns, contains the number of bytes that were used in the conversion. This parameter is passed uninitialized.
When this method returns, contains the number of characters from that were produced by the conversion. This parameter is passed uninitialized.
When this method returns, contains if all the characters specified by were converted; otherwise, . This parameter is passed uninitialized.
Converts an array of encoded bytes to UTF-16 encoded characters and stores the result in a character array.
object that has not been stored in the `bytes` buffer.
## Examples
The following example uses the method to convert a file of UTF-16 characters to UTF-8. It then uses the method to convert the UTF-8 characters back to UTF-16 characters.
:::code language="csharp" source="~/snippets/csharp/System.Text/Decoder/Convert/edCvt.cs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/System.Text/Decoder/Convert/edCvt.vb" id="Snippet1":::
]]>
or is ().
, , , or is less than zero.
-or-
The length of - is less than .
-or-
The length of - is less than .
The output buffer is too small to contain any of the converted input. The output buffer should be at least 2 chars in size to accomodate at least one surrogate character pair.
A fallback occurred (for more information, see Character Encoding in .NET)
-and-
is set to .
Property
System.Text.Encoding
4.0.10.0
mscorlib
2.0.0.0
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
System.Runtime
4.2.0.0
4.2.1.0
4.2.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
[System.Runtime.CompilerServices.Nullable(2)]
[<System.Runtime.CompilerServices.Nullable(2)>]
[System.Runtime.InteropServices.ComVisible(false)]
[<System.Runtime.InteropServices.ComVisible(false)>]
System.Text.DecoderFallback
Gets or sets a object for the current object.
A object.
object represents an error handler that is invoked when an encoded byte sequence cannot be converted to a character.
]]>
The value in a set operation is ().
A new value cannot be assigned in a set operation because the current object contains data that has not been decoded yet.
Understanding Encodings
Property
System.Text.Encoding
4.0.10.0
mscorlib
2.0.0.0
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
System.Runtime
4.2.0.0
4.2.1.0
4.2.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
[System.Runtime.CompilerServices.Nullable(1)]
[<System.Runtime.CompilerServices.Nullable(1)>]
[System.Runtime.InteropServices.ComVisible(false)]
[<System.Runtime.InteropServices.ComVisible(false)>]
System.Text.DecoderFallbackBuffer
Gets the object associated with the current object.
A object.
object represents data used by the object. The object represents an error handler that is invoked when an encoded byte sequence cannot be converted to a character.
]]>
mscorlib
2.0.5.0
4.0.0.0
When overridden in a derived class, calculates the number of characters produced by decoding a sequence of bytes.
Method
System.Runtime
4.2.1.0
4.2.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
mscorlib
netstandard
2.1.0.0
System.Text.Encoding
System.Int32
A byte span to decode.
to simulate clearing the internal state of the encoder after the calculation; otherwise, .
When overridden in a derived class, calculates the number of characters produced by decoding the sequence of bytes in the span. A parameter indicates whether to clear the internal state of the decoder after the calculation.
The number of characters produced by decoding the specified sequence of bytes and any bytes in the internal buffer.
requires to store the resulting characters, the application should use .
If `GetChars` is called with `flush` set to `false`, the decoder stores trailing bytes at the end of the data block in an internal buffer and uses them in the next decoding operation. The application should call `GetCharCount` on a block of data immediately before calling `GetChars` on the same block, so that any trailing bytes from the previous block are included in the calculation.
]]>
Method
mscorlib
2.0.0.0
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
System.Runtime
4.2.0.0
4.2.1.0
4.2.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
System.Text.Encoding
[System.CLSCompliant(false)]
[<System.CLSCompliant(false)>]
[System.Runtime.InteropServices.ComVisible(false)]
[<System.Runtime.InteropServices.ComVisible(false)>]
[System.Security.SecurityCritical]
[<System.Security.SecurityCritical>]
System.Int32
A pointer to the first byte to decode.
The number of bytes to decode.
to simulate clearing the internal state of the encoder after the calculation; otherwise, .
When overridden in a derived class, calculates the number of characters produced by decoding a sequence of bytes starting at the specified byte pointer. A parameter indicates whether to clear the internal state of the decoder after the calculation.
The number of characters produced by decoding the specified sequence of bytes and any bytes in the internal buffer.
requires to store the resulting characters, the application should use .
If `GetChars` is called with `flush` set to `false`, the decoder stores trailing bytes at the end of the data block in an internal buffer and uses them in the next decoding operation. The application should call `GetCharCount` on a block of data immediately before calling `GetChars` on the same block, so that any trailing bytes from the previous block are included in the calculation.
]]>
is ( in Visual Basic .NET).
is less than zero.
A fallback occurred (for more information, see Character Encoding in .NET)
-and-
is set to .
Method
System.Text.Encoding
4.0.0.0
4.0.10.0
mscorlib
1.0.5000.0
2.0.0.0
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
System.Runtime
4.2.0.0
4.2.1.0
4.2.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
System.Int32
The byte array containing the sequence of bytes to decode.
The index of the first byte to decode.
The number of bytes to decode.
When overridden in a derived class, calculates the number of characters produced by decoding a sequence of bytes from the specified byte array.
The number of characters produced by decoding the specified sequence of bytes and any bytes in the internal buffer.
requires to store the resulting characters, the application should use .
If `GetChars` is called with `flush` set to `false`, the decoder stores trailing bytes at the end of the data block in an internal buffer and uses them in the next decoding operation. The application should call `GetCharCount` on a block of data immediately before calling `GetChars` on the same block, so that any trailing bytes from the previous block are included in the calculation.
## Examples
The following code example demonstrates how to use the method to calculate the number of characters required to decode the specified range of bytes in the array.
:::code language="csharp" source="~/snippets/csharp/System.Text/Decoder/GetCharCount/getcharcount-byte[]-int32-int32.cs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/System.Text/Decoder/GetCharCount/getcharcount-byte[]-int32-int32.vb" id="Snippet1":::
]]>
is ().
or is less than zero.
-or-
and do not denote a valid range in .
A fallback occurred (for more information, see Character Encoding in .NET)
-and-
is set to .
Method
System.Text.Encoding
4.0.0.0
4.0.10.0
mscorlib
2.0.0.0
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
System.Runtime
4.2.0.0
4.2.1.0
4.2.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
[System.Runtime.InteropServices.ComVisible(false)]
[<System.Runtime.InteropServices.ComVisible(false)>]
System.Int32
The byte array containing the sequence of bytes to decode.
The index of the first byte to decode.
The number of bytes to decode.
to simulate clearing the internal state of the encoder after the calculation; otherwise, .
When overridden in a derived class, calculates the number of characters produced by decoding a sequence of bytes from the specified byte array. A parameter indicates whether to clear the internal state of the decoder after the calculation.
The number of characters produced by decoding the specified sequence of bytes and any bytes in the internal buffer.
requires to store the resulting characters, the application should use .
If `GetChars` is called with `flush` set to `false`, the decoder stores trailing bytes at the end of the data block in an internal buffer and uses them in the next decoding operation. The application should call `GetCharCount` on a block of data immediately before calling `GetChars` on the same block, so that any trailing bytes from the previous block are included in the calculation.
]]>
is ().
or is less than zero.
-or-
and do not denote a valid range in .
A fallback occurred (for more information, see Character Encoding in .NET)
-and-
is set to .
mscorlib
2.0.5.0
4.0.0.0
When overridden in a derived class, decodes a sequence of bytes into a set of characters.
Method
System.Runtime
4.2.1.0
4.2.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
mscorlib
netstandard
2.1.0.0
System.Text.Encoding
System.Int32
A byte span to decode.
A span to write the resulting set of characters.
to clear the internal state of the decoder after the conversion; otherwise, .
When overridden in a derived class, decodes a sequence of span bytes and any bytes in the internal buffer into a set of characters that are stored starting at the specified character pointer. A parameter indicates whether to clear the internal state of the decoder after the conversion.
The actual number of characters written at the span indicated by the parameter.
object saves state between calls to . When the application is done with a stream of data, it should set the `flush` parameter to `true` to make sure that the state information is flushed. With this setting, the decoder ignores invalid bytes at the end of the data block and clears the internal buffer.
To calculate the exact span size that `GetChars` requires to store the resulting characters, the application should use .
If `GetChars` is called with `flush` set to `false`, the decoder stores trailing bytes at the end of the data block in an internal buffer and uses them in the next decoding operation. The application should call `GetCharCount` on a block of data immediately before calling `GetChars` on the same block, so that any trailing bytes from the previous block are included in the calculation.
If your application is to convert many segments of an input stream, consider using the method. will throw an exception if the output span isn't large enough, but will fill as much space as possible and return the bytes read and chars written, provided the output array allows for at least two characters. Also see the topic for more comments.
]]>
Method
mscorlib
2.0.0.0
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
System.Runtime
4.2.0.0
4.2.1.0
4.2.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
System.Text.Encoding
[System.CLSCompliant(false)]
[<System.CLSCompliant(false)>]
[System.Runtime.InteropServices.ComVisible(false)]
[<System.Runtime.InteropServices.ComVisible(false)>]
[System.Security.SecurityCritical]
[<System.Security.SecurityCritical>]
System.Int32
A pointer to the first byte to decode.
The number of bytes to decode.
A pointer to the location at which to start writing the resulting set of characters.
The maximum number of characters to write.
to clear the internal state of the decoder after the conversion; otherwise, .
When overridden in a derived class, decodes a sequence of bytes starting at the specified byte pointer and any bytes in the internal buffer into a set of characters that are stored starting at the specified character pointer. A parameter indicates whether to clear the internal state of the decoder after the conversion.
The actual number of characters written at the location indicated by the parameter.
object saves state between calls to . When the application is done with a stream of data, it should set the `flush` parameter to `true` to make sure that the state information is flushed. With this setting, the decoder ignores invalid bytes at the end of the data block and clears the internal buffer.
To calculate the exact buffer size that `GetChars` requires to store the resulting characters, the application should use .
If `GetChars` is called with `flush` set to `false`, the decoder stores trailing bytes at the end of the data block in an internal buffer and uses them in the next decoding operation. The application should call `GetCharCount` on a block of data immediately before calling `GetChars` on the same block, so that any trailing bytes from the previous block are included in the calculation.
If your application is to convert many segments of an input stream, consider using the method. will throw an exception if the output buffer isn't large enough, but will fill as much space as possible and return the bytes read and chars written, provided the output array allows for at least two characters. Also see the topic for more comments.
]]>
is ().
-or-
is ().
or is less than zero.
is less than the resulting number of characters.
A fallback occurred (for more information, see Character Encoding in .NET)
-and-
is set to .
Method
System.Text.Encoding
4.0.0.0
4.0.10.0
mscorlib
1.0.5000.0
2.0.0.0
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
System.Runtime
4.2.0.0
4.2.1.0
4.2.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
System.Int32
The byte array containing the sequence of bytes to decode.
The index of the first byte to decode.
The number of bytes to decode.
The character array to contain the resulting set of characters.
The index at which to start writing the resulting set of characters.
When overridden in a derived class, decodes a sequence of bytes from the specified byte array and any bytes in the internal buffer into the specified character array.
The actual number of characters written into .
object saves state between calls to . When the application is done with a stream of data, it should set the `flush` parameter to `true` to make sure that the state information is flushed. With this setting, the decoder ignores invalid bytes at the end of the data block and clears the internal buffer.
To calculate the exact array size that `GetChars` requires to store the resulting characters, the application should use .
If `GetChars` is called with `flush` set to `false`, the decoder stores trailing bytes at the end of the data block in an internal buffer and uses them in the next decoding operation. The application should call `GetCharCount` on a block of data immediately before calling `GetChars` on the same block, so that any trailing bytes from the previous block are included in the calculation.
If your application is to convert many segments of an input stream, consider using the method. will throw an exception if the output buffer isn't large enough, but will fill as much space as possible and return the bytes read and chars written, provided the output array allows for at least two characters. Also see the topic for more comments.
## Examples
The following example demonstrates how to decode a range of elements from a byte array and store them in a Unicode character array. The method is used to calculate the number of characters needed to store the decoded elements in the array `bytes`. The method decodes the specified elements in the byte array and stores them in the new character array.
:::code language="csharp" source="~/snippets/csharp/System.Text/Decoder/GetChars/getchars-byte[]-int32-int32-char[]-int32.cs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/System.Text/Decoder/GetChars/getchars-byte[]-int32-int32-char[]-int32.vb" id="Snippet1":::
]]>
is ().
-or-
is ().
or or is less than zero.
-or-
and do not denote a valid range in .
-or-
is not a valid index in .
does not have enough capacity from to the end of the array to accommodate the resulting characters.
A fallback occurred (for more information, see Character Encoding in .NET)
-and-
is set to .
Method
System.Text.Encoding
4.0.0.0
4.0.10.0
mscorlib
2.0.0.0
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
System.Runtime
4.2.0.0
4.2.1.0
4.2.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
System.Int32
The byte array containing the sequence of bytes to decode.
The index of the first byte to decode.
The number of bytes to decode.
The character array to contain the resulting set of characters.
The index at which to start writing the resulting set of characters.
to clear the internal state of the decoder after the conversion; otherwise, .
When overridden in a derived class, decodes a sequence of bytes from the specified byte array and any bytes in the internal buffer into the specified character array. A parameter indicates whether to clear the internal state of the decoder after the conversion.
The actual number of characters written into the parameter.
object saves state between calls to . When the application is done with a stream of data, it should set the `flush` parameter to `true` to make sure that the state information is flushed. With this setting, the decoder ignores invalid bytes at the end of the data block and clears the internal buffer.
To calculate the exact array size that `GetChars` requires to store the resulting characters, the application should use .
If `GetChars` is called with `flush` set to `false`, the decoder stores trailing bytes at the end of the data block in an internal buffer and uses them in the next decoding operation. The application should call `GetCharCount` on a block of data immediately before calling `GetChars` on the same block, so that any trailing bytes from the previous block are included in the calculation.
If your application is to convert many segments of an input stream, consider using the method. will throw an exception if the output buffer isn't large enough, but will fill as much space as possible and return the bytes read and chars written, provided the output array allows for at least two characters. Also see the topic for more comments.
]]>
is ().
-or-
is ().
or or is less than zero.
-or-
and do not denote a valid range in .
-or-
is not a valid index in .
does not have enough capacity from to the end of the array to accommodate the resulting characters.
A fallback occurred (for more information, see Character Encoding in .NET)
-and-
is set to .
Method
System.Text.Encoding
4.0.0.0
4.0.10.0
mscorlib
2.0.0.0
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
System.Runtime
4.2.0.0
4.2.1.0
4.2.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
[System.Runtime.InteropServices.ComVisible(false)]
[<System.Runtime.InteropServices.ComVisible(false)>]
System.Void
When overridden in a derived class, sets the decoder back to its initial state.
object. The method clears any state information preserved from a previous call to or , including trailing bytes at the end of the previous data block.
Your application should call the method if it wants to reuse the same decoder even after an exception is thrown by , , or , or if the decoder switches streams and begins to decode another stream.
]]>