Encoder.Convert Método

Definición

Convierte un búfer o una matriz de caracteres Unicode en una secuencia de bytes codificada.Converts a buffer or array of Unicode characters to an encoded byte sequence.

Sobrecargas

Convert(ReadOnlySpan<Char>, Span<Byte>, Boolean, Int32, Int32, Boolean)

Convierte un intervalo de caracteres Unicode en una secuencia de bytes codificada y almacena el resultado en otro búfer.Converts a span of Unicode characters to an encoded byte sequence and stores the result in another buffer.

Convert(Char*, Int32, Byte*, Int32, Boolean, Int32, Int32, Boolean)

Convierte un búfer de caracteres Unicode en una secuencia de bytes codificada y almacena el resultado en otro búfer.Converts a buffer of Unicode characters to an encoded byte sequence and stores the result in another buffer.

Convert(Char[], Int32, Int32, Byte[], Int32, Int32, Boolean, Int32, Int32, Boolean)

Convierte una matriz de caracteres Unicode en una secuencia de bytes codificada y almacena el resultado en otra matriz de bytes.Converts an array of Unicode characters to an encoded byte sequence and stores the result in an array of bytes.

Convert(ReadOnlySpan<Char>, Span<Byte>, Boolean, Int32, Int32, Boolean)

Convierte un intervalo de caracteres Unicode en una secuencia de bytes codificada y almacena el resultado en otro búfer.Converts a span of Unicode characters to an encoded byte sequence and stores the result in another buffer.

public:
 virtual void Convert(ReadOnlySpan<char> chars, Span<System::Byte> bytes, bool flush, [Runtime::InteropServices::Out] int % charsUsed, [Runtime::InteropServices::Out] int % bytesUsed, [Runtime::InteropServices::Out] bool % completed);
public virtual void Convert (ReadOnlySpan<char> chars, Span<byte> bytes, bool flush, out int charsUsed, out int bytesUsed, out bool completed);
abstract member Convert : ReadOnlySpan<char> * Span<byte> * bool * int * int * bool -> unit
override this.Convert : ReadOnlySpan<char> * Span<byte> * bool * int * int * bool -> unit
Public Overridable Sub Convert (chars As ReadOnlySpan(Of Char), bytes As Span(Of Byte), flush As Boolean, ByRef charsUsed As Integer, ByRef bytesUsed As Integer, ByRef completed As Boolean)

Parámetros

chars
ReadOnlySpan<Char>

El intervalo de caracteres con codificación UTF-16 que se va a convertir.The span of UTF-16 encoded characters to convert.

bytes
Span<Byte>

El búfer de intervalo donde se van a almacenar los bytes convertidos.The span buffer to store the converted bytes.

flush
Boolean

true para indicar que no se van a convertir más datos; de lo contrario, false.true to indicate no further data is to be converted; otherwise, false.

charsUsed
Int32

Cuando este método vuelve, contiene el número de caracteres de chars que se usaron en la conversión.When this method returns, contains the number of characters from chars that were used in the conversion. Este parámetro se pasa sin inicializar.This parameter is passed uninitialized.

bytesUsed
Int32

Cuando este método vuelve, contiene el número de bytes que se usaron en la conversión.When this method returns, contains the number of bytes that were used in the conversion. Este parámetro se pasa sin inicializar.This parameter is passed uninitialized.

completed
Boolean

Cuando este método devuelve un valor, contiene true si se han convertido todos los caracteres de chars; de lo contrario, false.When this method returns, contains true if all the characters in chars were converted; otherwise, false. Este parámetro se pasa sin inicializar.This parameter is passed uninitialized.

Comentarios

Recuerde que el objeto Encoder guarda el estado entre las llamadas a Convert.Remember that the Encoder object saves state between calls to Convert. Cuando la aplicación se realiza con un flujo de datos, debe establecer el parámetro flush en true para asegurarse de que se vacía la información de estado.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. Con esta configuración, el codificador omite los bytes no válidos al final del bloque de datos y borra el búfer interno.With this setting, the encoder ignores invalid bytes at the end of the data block and clears the internal buffer. Los datos procesados restantes que formen parte de una unidad lógica, como el suplente alto de un par suplente, se convertirán según la configuración de reserva actual.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.

El método Convert está diseñado para usarse en un bucle con el fin de codificar una cantidad arbitraria de entradas, como datos leídos de un archivo o una secuencia.The Convert method is designed to be used in a loop to encode an arbitrary amount of input, such as data read from a file or stream. Almacena la salida de la operación de codificación en un búfer de tamaño fijo.It stores the output of the encoding operation in a fixed-size buffer. GetBytes producirá una excepción si el búfer de salida no es suficientemente grande, pero Convert rellenará tanto espacio como sea posible y devolverá los caracteres leídos y bytes escritos.GetBytes will throw an exception if the output buffer isn't large enough, but Convert will fill as much space as possible and return the chars read and bytes written. Vea también el tema Encoding.GetBytes para obtener más comentarios.Also see the Encoding.GetBytes topic for more comments.

El parámetro de salida completed indica si todos los datos del búfer de entrada se han convertido y almacenado en el búfer de salida.The completed output parameter indicates whether all the data in the input buffer was converted and stored in the output buffer. Este parámetro se establece en false si no se puede convertir el número de caracteres especificado por el parámetro charCount sin superar el número de bytes especificado por el parámetro byteCount.This parameter is set to false if the number of characters specified by the charCount parameter cannot be converted without exceeding the number of bytes specified by the byteCount parameter. En esta situación, la aplicación debe utilizar el contenido del búfer de salida o proporcionar un nuevo búfer de salida, incrementar el parámetro chars por el número de caracteres especificado por el parámetro charsUsed y, a continuación, llamar de nuevo al método Convert para procesar el entrada restante.In that situation, the application should use the contents of the output buffer or provide a new output buffer, increment the chars parameter by the number of characters specified by the charsUsed parameter, then call the Convert method again to process the remaining input.

El parámetro completed también se puede establecer en false, aunque el parámetro charsUsed y la longitud del intervalo ' char ' son iguales.The completed parameter can also be set to false, even though the charsUsed parameter and 'char' span length are equal. Esta situación se produce si todavía hay datos en el Encoder objeto que no se ha almacenado en el búfer de chars.This situation occurs if there is still data in the Encoder object that has not been stored in the chars buffer.

Convert(Char*, Int32, Byte*, Int32, Boolean, Int32, Int32, Boolean)

Importante

Esta API no es conforme a CLS.

Alternativa conforme a CLS
System.Text.Encoder.Convert(Char[], Int32, Int32, Byte[], Int32, Int32, Boolean, Int32, Int32, Boolean)

Convierte un búfer de caracteres Unicode en una secuencia de bytes codificada y almacena el resultado en otro búfer.Converts a buffer of Unicode characters to an encoded byte sequence and stores the result in another buffer.

public:
 virtual void Convert(char* chars, int charCount, System::Byte* bytes, int byteCount, bool flush, [Runtime::InteropServices::Out] int % charsUsed, [Runtime::InteropServices::Out] int % bytesUsed, [Runtime::InteropServices::Out] bool % completed);
[System.CLSCompliant(false)]
[System.Runtime.InteropServices.ComVisible(false)]
[System.Security.SecurityCritical]
public virtual void Convert (char* chars, int charCount, byte* bytes, int byteCount, bool flush, out int charsUsed, out int bytesUsed, out bool completed);
abstract member Convert : nativeptr<char> * int * nativeptr<byte> * int * bool * int * int * bool -> unit
override this.Convert : nativeptr<char> * int * nativeptr<byte> * int * bool * int * int * bool -> unit

Parámetros

chars
Char*

Dirección de una cadena de caracteres con codificación UTF-16 que se va a convertir.The address of a string of UTF-16 encoded characters to convert.

charCount
Int32

Número de caracteres de chars que se van a convertir.The number of characters in chars to convert.

bytes
Byte*

Dirección de un búfer donde se van a almacenar los bytes convertidos.The address of a buffer to store the converted bytes.

byteCount
Int32

Número máximo de bytes en bytes que se puede usar en la conversión.The maximum number of bytes in bytes to use in the conversion.

flush
Boolean

true para indicar que no se van a convertir más datos; de lo contrario, false.true to indicate no further data is to be converted; otherwise, false.

charsUsed
Int32

Cuando este método vuelve, contiene el número de caracteres de chars que se usaron en la conversión.When this method returns, contains the number of characters from chars that were used in the conversion. Este parámetro se pasa sin inicializar.This parameter is passed uninitialized.

bytesUsed
Int32

Cuando este método vuelve, contiene el número de bytes que se usaron en la conversión.When this method returns, contains the number of bytes that were used in the conversion. Este parámetro se pasa sin inicializar.This parameter is passed uninitialized.

completed
Boolean

Cuando este método vuelve, contiene true si todos los caracteres especificados por charCount se han convertido; de lo contrario, false.When this method returns, contains true if all the characters specified by charCount were converted; otherwise, false. Este parámetro se pasa sin inicializar.This parameter is passed uninitialized.

Atributos

Excepciones

chars o bytes es null (Nothing).chars or bytes is null (Nothing).

charCount o byteCount es menor que cero.charCount or byteCount is less than zero.

El búfer de salida es demasiado pequeño para contener la entrada convertida.The output buffer is too small to contain any of the converted input. El búfer de salida debe ser mayor o igual que el tamaño indicado por el método GetByteCount.The output buffer should be greater than or equal to the size indicated by the GetByteCount method.

Se ha producido una reserva (para más información, vea Codificación de caracteres en .NET)A fallback occurred (for more information, see Character Encoding in .NET)

- y --and-

El valor de Fallback está establecido en EncoderExceptionFallback.Fallback is set to EncoderExceptionFallback.

Comentarios

Recuerde que el objeto Encoder guarda el estado entre las llamadas a Convert.Remember that the Encoder object saves state between calls to Convert. Cuando la aplicación se realiza con un flujo de datos, debe establecer el parámetro flush en true para asegurarse de que se vacía la información de estado.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. Con esta configuración, el codificador omite los bytes no válidos al final del bloque de datos y borra el búfer interno.With this setting, the encoder ignores invalid bytes at the end of the data block and clears the internal buffer. Los datos procesados restantes que formen parte de una unidad lógica, como el suplente alto de un par suplente, se convertirán según la configuración de reserva actual.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.

El método Convert está diseñado para usarse en un bucle con el fin de codificar una cantidad arbitraria de entradas, como datos leídos de un archivo o una secuencia.The Convert method is designed to be used in a loop to encode an arbitrary amount of input, such as data read from a file or stream. Almacena la salida de la operación de codificación en un búfer de tamaño fijo.It stores the output of the encoding operation in a fixed-size buffer. GetBytes producirá una excepción si el búfer de salida no es suficientemente grande, pero Convert rellenará tanto espacio como sea posible y devolverá los caracteres leídos y bytes escritos.GetBytes will throw an exception if the output buffer isn't large enough, but Convert will fill as much space as possible and return the chars read and bytes written. Vea también el tema Encoding.GetBytes para obtener más comentarios.Also see the Encoding.GetBytes topic for more comments.

El parámetro de salida completed indica si todos los datos del búfer de entrada se han convertido y almacenado en el búfer de salida.The completed output parameter indicates whether all the data in the input buffer was converted and stored in the output buffer. Este parámetro se establece en false si no se puede convertir el número de caracteres especificado por el parámetro charCount sin superar el número de bytes especificado por el parámetro byteCount.This parameter is set to false if the number of characters specified by the charCount parameter cannot be converted without exceeding the number of bytes specified by the byteCount parameter. En esta situación, la aplicación debe utilizar el contenido del búfer de salida o proporcionar un nuevo búfer de salida, incrementar el parámetro chars por el número de caracteres especificado por el parámetro charsUsed y, a continuación, llamar de nuevo al método Convert para procesar el entrada restante.In that situation, the application should use the contents of the output buffer or provide a new output buffer, increment the chars parameter by the number of characters specified by the charsUsed parameter, then call the Convert method again to process the remaining input.

El parámetro completed también se puede establecer en false, aunque los parámetros charsUsed y charCount sean iguales.The completed parameter can also be set to false, even though the charsUsed and charCount parameters are equal. Esta situación se produce si todavía hay datos en el Encoder objeto que no se ha almacenado en el búfer de chars.This situation occurs if there is still data in the Encoder object that has not been stored in the chars buffer.

Seguridad

SecurityCriticalAttribute
Requiere plena confianza para el llamador inmediato.Requires full trust for the immediate caller. Este miembro no puede ser utilizado por código transparente o de confianza parcial.This member cannot be used by partially trusted or transparent code.

Convert(Char[], Int32, Int32, Byte[], Int32, Int32, Boolean, Int32, Int32, Boolean)

Convierte una matriz de caracteres Unicode en una secuencia de bytes codificada y almacena el resultado en otra matriz de bytes.Converts an array of Unicode characters to an encoded byte sequence and stores the result in an array of bytes.

public:
 virtual void Convert(cli::array <char> ^ chars, int charIndex, int charCount, cli::array <System::Byte> ^ bytes, int byteIndex, int byteCount, bool flush, [Runtime::InteropServices::Out] int % charsUsed, [Runtime::InteropServices::Out] int % bytesUsed, [Runtime::InteropServices::Out] bool % completed);
[System.Runtime.InteropServices.ComVisible(false)]
public virtual void Convert (char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex, int byteCount, bool flush, out int charsUsed, out int bytesUsed, out bool completed);
abstract member Convert : char[] * int * int * byte[] * int * int * bool * int * int * bool -> unit
override this.Convert : char[] * int * int * byte[] * int * int * bool * int * int * bool -> unit
Public Overridable Sub Convert (chars As Char(), charIndex As Integer, charCount As Integer, bytes As Byte(), byteIndex As Integer, byteCount As Integer, flush As Boolean, ByRef charsUsed As Integer, ByRef bytesUsed As Integer, ByRef completed As Boolean)

Parámetros

chars
Char[]

Matriz de caracteres que se va a convertir.An array of characters to convert.

charIndex
Int32

Primer elemento de chars que se va a convertir.The first element of chars to convert.

charCount
Int32

Número de elementos de chars que se van a convertir.The number of elements of chars to convert.

bytes
Byte[]

Matriz donde se almacenan los bytes convertidos.An array where the converted bytes are stored.

byteIndex
Int32

El primer elemento de bytes en el que se almacenan los datos.The first element of bytes in which data is stored.

byteCount
Int32

Número máximo de elementos de bytes que se puede usar en la conversión.The maximum number of elements of bytes to use in the conversion.

flush
Boolean

true para indicar que no se van a convertir más datos; de lo contrario, false.true to indicate no further data is to be converted; otherwise, false.

charsUsed
Int32

Cuando este método vuelve, contiene el número de caracteres de chars que se usaron en la conversión.When this method returns, contains the number of characters from chars that were used in the conversion. Este parámetro se pasa sin inicializar.This parameter is passed uninitialized.

bytesUsed
Int32

Cuando este método vuelve, contiene el número de bytes que ha generado la conversión.When this method returns, contains the number of bytes that were produced by the conversion. Este parámetro se pasa sin inicializar.This parameter is passed uninitialized.

completed
Boolean

Cuando este método vuelve, contiene true si todos los caracteres especificados por charCount se han convertido; de lo contrario, false.When this method returns, contains true if all the characters specified by charCount were converted; otherwise, false. Este parámetro se pasa sin inicializar.This parameter is passed uninitialized.

Atributos

Excepciones

chars o bytes es null (Nothing).chars or bytes is null (Nothing).

charIndex, charCount, byteIndexo byteCount es menor que cero.charIndex, charCount, byteIndex, or byteCount is less than zero.

O bien-or-

La longitud de chars - charIndex es menor que charCount.The length of chars - charIndex is less than charCount.

O bien-or-

La longitud de bytes - byteIndex es menor que byteCount.The length of bytes - byteIndex is less than byteCount.

El búfer de salida es demasiado pequeño para contener la entrada convertida.The output buffer is too small to contain any of the converted input. El búfer de salida debe ser mayor o igual que el tamaño indicado por el método GetByteCount.The output buffer should be greater than or equal to the size indicated by the GetByteCount method.

Se ha producido una reserva (para más información, vea Codificación de caracteres en .NET)A fallback occurred (for more information, see Character Encoding in .NET)

- y --and-

El valor de Fallback está establecido en EncoderExceptionFallback.Fallback is set to EncoderExceptionFallback.

Ejemplos

En el ejemplo siguiente se usa el método Convert para convertir un archivo de caracteres UTF-16 a UTF-8 y, a continuación, se usa el método Convert para volver a convertir los caracteres UTF-8 en caracteres UTF-16.The following example uses the Convert method to convert a file of UTF-16 characters to UTF-8, then uses the Convert method to convert the UTF-8 characters back to UTF-16 characters.

// This code example demonstrates the Encoder.Convert() and Decoder.Convert methods. 
// This example uses files for input and output, but any source that can be expressed
// as a stream can be used instead.

    using System;
    using System.Text;
    using System.IO;

    public class Sample
    {
    static void Main(string[] args)
        {
// Create a large file of UTF-16 encoded Unicode characters. The file is named Example.txt, 
// and is used as input to the Encoder.Convert() method. 

            CreateTestFile("Example.txt");

 
// Using an input file of UTF-16 encoded characters named Example.txt, create an output file 
// of UTF-8 encoded bytes named UTF8.txt.

            EncoderConvert("Example.txt", "UTF8.txt", Encoding.UTF8);

// Using an input file of UTF-8 encoded bytes named UTF8.txt, create an output file 
// of UTF-16 encoded characters named UTF16.txt.

            DecoderConvert("UTF8.txt", "UTF16.txt", Encoding.UTF8);
        }

// --------------------------------------------------------------------------------------------
// Use the Encoder.Convert() method to convert a file of characters to a file of encoded bytes.
// --------------------------------------------------------------------------------------------
        static void EncoderConvert(String inputFileName, String outputFileName, Encoding enc)
        {
// Convert an input file of characters to an output file of encoded bytes.
// StreamWriter could convert the input file for us, but we'll perform the conversion 
// ourselves.

            FileStream fs = new FileStream(outputFileName, FileMode.Create);
            BinaryWriter outputFile = new BinaryWriter(fs);

// StreamReader will detect Unicode encoding from the Byte Order Mark that heads the input file.
            StreamReader inputFile = new StreamReader(inputFileName);

// Get an Encoder.
            Encoder encoder = enc.GetEncoder();
            
// Guarantee the output buffer large enough to convert a few characters.
            int UseBufferSize = 64;
            if (UseBufferSize < enc.GetMaxByteCount(10))
                    UseBufferSize = enc.GetMaxByteCount(10);
            byte[] bytes = new byte[UseBufferSize];

// Intentionally make the input character buffer larger than the output byte buffer so the 
// conversion loop executes more than one cycle. 

            char[] chars = new char[UseBufferSize * 4];
            int charsRead;
            do
            {
// Read at most the number of characters that will fit in the input buffer. The return 
// value is the actual number of characters read, or zero if no characters remain. 
                charsRead = inputFile.Read(chars, 0, UseBufferSize * 4);
   
                bool completed = false;
                int charIndex = 0;
                int charsUsed;
                int bytesUsed;

                while (!completed)
                {
// If this is the last input data, flush the encoder's internal buffer and state.

                    bool flush = (charsRead == 0);
                    encoder.Convert(chars, charIndex, charsRead - charIndex,
                                    bytes, 0, UseBufferSize, flush,
                                    out charsUsed, out bytesUsed, out completed);

// The conversion produced the number of bytes indicated by bytesUsed. Write that number
// of bytes to the output file.
                    outputFile.Write(bytes, 0, bytesUsed);

// Increment charIndex to the next block of characters in the input buffer, if any, to convert.
                    charIndex += charsUsed;
                }
            }
            while(charsRead != 0);

            outputFile.Close();
            fs.Close();
            inputFile.Close();
        }

// --------------------------------------------------------------------------------------------
// Use the Decoder.Convert() method to convert a file of encoded bytes to a file of characters.
// --------------------------------------------------------------------------------------------
        static void DecoderConvert(String inputFileName, String outputFileName, Encoding enc)
        {
// Convert an input file of of encoded bytes to an output file characters.
// StreamWriter could convert the input file for us, but we'll perform the conversion 
// ourselves.

            StreamWriter outputFile = new StreamWriter(outputFileName, false, Encoding.Unicode);

// Read the input as a binary file so we can detect the Byte Order Mark.
            FileStream fs = new FileStream(inputFileName, FileMode.Open);
            BinaryReader inputFile = new BinaryReader(fs);

// Get a Decoder.
            Decoder decoder = enc.GetDecoder();
            
// Guarantee the output buffer large enough to convert a few characters.
            int UseBufferSize = 64;
            if (UseBufferSize < enc.GetMaxCharCount(10))
                    UseBufferSize = enc.GetMaxCharCount(10);
            char[] chars = new char[UseBufferSize];

// Intentionally make the input byte buffer larger than the output character buffer so the 
// conversion loop executes more than one cycle. 

            byte[] bytes = new byte[UseBufferSize * 4];
            int bytesRead;
            do
            {
// Read at most the number of bytes that will fit in the input buffer. The 
// return value is the actual number of bytes read, or zero if no bytes remain. 

                bytesRead = inputFile.Read(bytes, 0, UseBufferSize * 4);
   
                bool completed = false;
                int byteIndex = 0;
                int bytesUsed;
                int charsUsed;

                while (!completed)
                {
// If this is the last input data, flush the decoder's internal buffer and state.

                    bool flush = (bytesRead == 0);
                    decoder.Convert(bytes, byteIndex, bytesRead - byteIndex,
                                    chars, 0, UseBufferSize, flush,
                                    out bytesUsed, out charsUsed, out completed);

// The conversion produced the number of characters indicated by charsUsed. Write that number
// of characters to the output file.

                    outputFile.Write(chars, 0, charsUsed);

// Increment byteIndex to the next block of bytes in the input buffer, if any, to convert.
                    byteIndex += bytesUsed;
                }
            }
            while(bytesRead != 0);

            outputFile.Close();
            fs.Close();
            inputFile.Close();
        }        

// --------------------------------------------------------------------------------------------
// Create a large file of UTF-16 encoded Unicode characters. 
// --------------------------------------------------------------------------------------------
        static void CreateTestFile(String FileName)
        {
// StreamWriter defaults to UTF-8 encoding so explicitly specify Unicode, that is, 
// UTF-16, encoding.
            StreamWriter file = new StreamWriter(FileName, false, Encoding.Unicode);

// Write a line of text 100 times.
            for (int i = 0; i < 100; i++)
            {
                file.WriteLine("This is an example input file used by the convert example.");
            }

// Write Unicode characters from U+0000 to, but not including, the surrogate character range.
            for (char c = (char)0; c < (char)0xD800; c++)
            {
                file.Write(c);
            }
            file.Close();
        }
    }

/*
This code example produces the following results:

(Execute the -dir- console window command and examine the files created.)

Example.txt, which contains 122,594 bytes (61,297 UTF-16 encoded characters).
UTF8.txt, which contains 169,712 UTF-8 encoded bytes.
UTF16.txt, which contains 122,594 bytes (61,297 UTF-16 encoded characters).

(Execute the -comp- console window command and compare the two Unicode files.)

>comp example.txt utf16.txt /L
Comparing example.txt and utf16.txt...
Files compare OK

(The two files are equal.)

*/
' This code example demonstrates the Encoder.Convert() and Decoder.Convert methods. 
' This example uses files for input and output, but any source that can be expressed
' as a stream can be used instead.

Imports System.Text
Imports System.IO

Public Class Sample
    Shared Sub Main(ByVal args() As String) 
        ' Create a large file of UTF-16 encoded Unicode characters. The file is named Example.txt, 
        ' and is used as input to the Encoder.Convert() method. 
        CreateTestFile("Example.txt")
        
        
        ' Using an input file of UTF-16 encoded characters named Example.txt, create an output file 
        ' of UTF-8 encoded bytes named UTF8.txt.
        EncoderConvert("Example.txt", "UTF8.txt", Encoding.UTF8)
        
        ' Using an input file of UTF-8 encoded bytes named UTF8.txt, create an output file 
        ' of UTF-16 encoded characters named UTF16.txt.
        DecoderConvert("UTF8.txt", "UTF16.txt", Encoding.UTF8)
    
    End Sub
    
    ' --------------------------------------------------------------------------------------------
    ' Use the Encoder.Convert() method to convert a file of characters to a file of encoded bytes.
    ' --------------------------------------------------------------------------------------------
    Shared Sub EncoderConvert(ByVal inputFileName As String, ByVal outputFileName As String, ByVal enc As Encoding) 
        ' Convert an input file of characters to an output file of encoded bytes.
        ' StreamWriter could convert the input file for us, but we'll perform the conversion 
        ' ourselves.
        Dim fs As New FileStream(outputFileName, FileMode.Create)
        Dim outputFile As New BinaryWriter(fs)
        
        ' StreamReader will detect Unicode encoding from the Byte Order Mark that heads the input file.
        Dim inputFile As New StreamReader(inputFileName)
        
        ' Get an Encoder.
        Dim encoder As Encoder = enc.GetEncoder()
        
        ' Guarantee the output buffer large enough to convert a few characters.
        Dim UseBufferSize As Integer = 64
        If UseBufferSize < enc.GetMaxByteCount(10) Then
            UseBufferSize = enc.GetMaxByteCount(10)
        End If
        Dim bytes(UseBufferSize) As Byte
        
        ' Intentionally make the input character buffer larger than the output byte buffer so the 
        ' conversion loop executes more than one cycle. 
        Dim chars(UseBufferSize * 4) As Char
        Dim charsRead As Integer
        Do
            ' Read at most the number of characters that will fit in the input buffer. The return 
            ' value is the actual number of characters read, or zero if no characters remain. 
            charsRead = inputFile.Read(chars, 0, UseBufferSize * 4)
            
            Dim completed As Boolean = False
            Dim charIndex As Integer = 0
            Dim charsUsed As Integer
            Dim bytesUsed As Integer
            
            While Not completed
                ' If this is the last input data, flush the encoder's internal buffer and state.
                Dim flush As Boolean = charsRead = 0
                encoder.Convert(chars, charIndex, charsRead - charIndex, bytes, 0, UseBufferSize, flush, charsUsed, bytesUsed, completed)
                
                ' The conversion produced the number of bytes indicated by bytesUsed. Write that number
                ' of bytes to the output file.
                outputFile.Write(bytes, 0, bytesUsed)
                
                ' Increment charIndex to the next block of characters in the input buffer, if any, to convert.
                charIndex += charsUsed
            End While
        Loop While charsRead <> 0
        
        outputFile.Close()
        fs.Close()
        inputFile.Close()
    
    End Sub
    
    ' --------------------------------------------------------------------------------------------
    ' Use the Decoder.Convert() method to convert a file of encoded bytes to a file of characters.
    ' --------------------------------------------------------------------------------------------
    Shared Sub DecoderConvert(ByVal inputFileName As String, ByVal outputFileName As String, ByVal enc As Encoding) 
        ' Convert an input file of of encoded bytes to an output file characters.
        ' StreamWriter could convert the input file for us, but we'll perform the conversion 
        ' ourselves.
        Dim outputFile As New StreamWriter(outputFileName, False, Encoding.Unicode)
        
        ' Read the input as a binary file so we can detect the Byte Order Mark.
        Dim fs As New FileStream(inputFileName, FileMode.Open)
        Dim inputFile As New BinaryReader(fs)
        
        ' Get a Decoder.
        Dim decoder As Decoder = enc.GetDecoder()
        
        ' Guarantee the output buffer large enough to convert a few characters.
        Dim UseBufferSize As Integer = 64
        If UseBufferSize < enc.GetMaxCharCount(10) Then
            UseBufferSize = enc.GetMaxCharCount(10)
        End If
        Dim chars(UseBufferSize) As Char
        
        ' Intentionally make the input byte buffer larger than the output character buffer so the 
        ' conversion loop executes more than one cycle. 
        Dim bytes(UseBufferSize * 4) As Byte
        Dim bytesRead As Integer
        Do
            ' Read at most the number of bytes that will fit in the input buffer. The 
            ' return value is the actual number of bytes read, or zero if no bytes remain. 
            bytesRead = inputFile.Read(bytes, 0, UseBufferSize * 4)
            
            Dim completed As Boolean = False
            Dim byteIndex As Integer = 0
            Dim bytesUsed As Integer
            Dim charsUsed As Integer
            
            While Not completed
                ' If this is the last input data, flush the decoder's internal buffer and state.
                Dim flush As Boolean = bytesRead = 0
                decoder.Convert(bytes, byteIndex, bytesRead - byteIndex, chars, 0, UseBufferSize, flush, bytesUsed, charsUsed, completed)
                
                ' The conversion produced the number of characters indicated by charsUsed. Write that number
                ' of characters to the output file.
                outputFile.Write(chars, 0, charsUsed)
                
                ' Increment byteIndex to the next block of bytes in the input buffer, if any, to convert.
                byteIndex += bytesUsed
            End While
        Loop While bytesRead <> 0
        
        outputFile.Close()
        fs.Close()
        inputFile.Close()
    
    End Sub
    
    ' --------------------------------------------------------------------------------------------
    ' Create a large file of UTF-16 encoded Unicode characters. 
    ' --------------------------------------------------------------------------------------------
    Shared Sub CreateTestFile(ByVal FileName As String) 
        ' StreamWriter defaults to UTF-8 encoding so explicitly specify Unicode, that is, 
        ' UTF-16, encoding.
        Dim file As New StreamWriter(FileName, False, Encoding.Unicode)
        
        ' Write a line of text 100 times.
        Dim i As Integer
        For i = 0 To 99
            file.WriteLine("This is an example input file used by the convert example.")
        Next i
        
        ' Write Unicode characters from U+0000 to, but not including, the surrogate character range.
        Dim c As Integer
        For c = 0 To &HD800
            file.Write(ChrW(c))
        Next c
        file.Close()
    
    End Sub
End Class

'
'This code example produces the following results:
'
'(Execute the -dir- console window command and examine the files created.)
'
'Example.txt, which contains 122,594 bytes (61,297 UTF-16 encoded characters).
'UTF8.txt, which contains 169,712 UTF-8 encoded bytes.
'UTF16.txt, which contains 122,594 bytes (61,297 UTF-16 encoded characters).
'
'(Execute the -comp- console window command and compare the two Unicode files.)
'
'>comp example.txt utf16.txt /L
'Comparing example.txt and utf16.txt...
'Files compare OK
'
'(The two files are equal.)
'

Comentarios

Recuerde que el objeto Encoder guarda el estado entre las llamadas a Convert.Remember that the Encoder object saves state between calls to Convert. Cuando la aplicación se realiza con un flujo de datos, debe establecer el parámetro flush en true para asegurarse de que se vacía la información de estado.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. Con esta configuración, el codificador omite los bytes no válidos al final del bloque de datos y borra el búfer interno.With this setting, the encoder ignores invalid bytes at the end of the data block and clears the internal buffer. Los datos procesados restantes que formen parte de una unidad lógica, como el suplente alto de un par suplente, se convertirán según la configuración de reserva actual.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.

El método Convert está diseñado para usarse en un bucle con el fin de codificar una cantidad arbitraria de entradas, como datos leídos de un archivo o una secuencia.The Convert method is designed to be used in a loop to encode an arbitrary amount of input, such as data read from a file or stream. Almacena la salida de la operación de codificación en un búfer de tamaño fijo.It stores the output of the encoding operation in a fixed-size buffer. GetBytes producirá una excepción si el búfer de salida no es suficientemente grande, pero Convert rellenará tanto espacio como sea posible y devolverá los caracteres leídos y bytes escritos.GetBytes will throw an exception if the output buffer isn't large enough, but Convert will fill as much space as possible and return the chars read and bytes written. Consulte también el artículo Encoding.GetBytes para obtener más comentarios.Also see the Encoding.GetBytes article for more comments.

El parámetro de salida completed indica si todos los datos del búfer de entrada se han convertido y almacenado en el búfer de salida.The completed output parameter indicates whether all the data in the input buffer was converted and stored in the output buffer. Este parámetro se establece en false si no se puede convertir el número de caracteres especificado por el parámetro charCount sin superar el número de bytes especificado por el parámetro byteCount.This parameter is set to false if the number of characters specified by the charCount parameter cannot be converted without exceeding the number of bytes specified by the byteCount parameter. En esta situación, la aplicación debe utilizar el contenido del búfer de salida o proporcionar un nuevo búfer de salida, incrementar el parámetro chars por el número de caracteres especificado por el parámetro charsUsed y, a continuación, llamar de nuevo al método Convert para procesar el entrada restante.In that situation, the application should use the contents of the output buffer or provide a new output buffer, increment the chars parameter by the number of characters specified by the charsUsed parameter, then call the Convert method again to process the remaining input.

El parámetro completed también se puede establecer en false, aunque los parámetros charsUsed y charCount sean iguales.The completed parameter can also be set to false, even though the charsUsed and charCount parameters are equal. Esta situación se produce si todavía hay datos en el Encoder objeto que no se ha almacenado en el búfer de chars.This situation occurs if there is still data in the Encoder object that has not been stored in the chars buffer.

Se aplica a