Encoder Encoder Encoder Encoder Class

Definición

Codifica un juego de caracteres en una secuencia de bytes.Converts a set of characters into a sequence of bytes.

public ref class Encoder abstract
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public abstract class Encoder
type Encoder = class
Public MustInherit Class Encoder
Herencia
EncoderEncoderEncoderEncoder
Atributos

Ejemplos

El ejemplo siguiente muestra cómo convertir una matriz de caracteres Unicode en bloques de bytes con una codificación especificada.The following example demonstrates how to convert an array of Unicode characters into blocks of bytes using a specified encoding. Para la comparación, la matriz de caracteres se codifica primero mediante UTF7Encoding.For comparison, the array of characters is first encoded using UTF7Encoding. A continuación, la matriz de caracteres se codifica mediante un Encoder.Next, the array of characters is encoded using an Encoder.

using namespace System;
using namespace System::Text;
using namespace System::Collections;
void ShowArray( Array^ theArray )
{
   IEnumerator^ myEnum = theArray->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Object^ o = safe_cast<Object^>(myEnum->Current);
      Console::Write( "[{0}]", o );
   }

   Console::WriteLine( "\n" );
}

int main()
{
   
   // The characters to encode.
   
   // Pi
   // Sigma
   array<Char>^chars = {L'\u03a0',L'\u03a3',L'\u03a6',L'\u03a9'};
   
   // Encode characters using an Encoding Object*.
   Encoding^ encoding = Encoding::UTF7;
   Console::WriteLine( "Using Encoding\n--------------" );
   
   // Encode complete array for comparison.
   array<Byte>^allCharactersFromEncoding = encoding->GetBytes( chars );
   Console::WriteLine( "All characters encoded:" );
   ShowArray( allCharactersFromEncoding );
   
   // Encode characters, one-by-one.
   // The Encoding Object* will NOT maintain state between calls.
   array<Byte>^firstchar = encoding->GetBytes( chars, 0, 1 );
   Console::WriteLine( "First character:" );
   ShowArray( firstchar );
   array<Byte>^secondchar = encoding->GetBytes( chars, 1, 1 );
   Console::WriteLine( "Second character:" );
   ShowArray( secondchar );
   array<Byte>^thirdchar = encoding->GetBytes( chars, 2, 1 );
   Console::WriteLine( "Third character:" );
   ShowArray( thirdchar );
   array<Byte>^fourthchar = encoding->GetBytes( chars, 3, 1 );
   Console::WriteLine( "Fourth character:" );
   ShowArray( fourthchar );
   
   // Now, encode characters using an Encoder Object*.
   Encoder^ encoder = encoding->GetEncoder();
   Console::WriteLine( "Using Encoder\n-------------" );
   
   // Encode complete array for comparison.
   array<Byte>^allCharactersFromEncoder = gcnew array<Byte>(encoder->GetByteCount( chars, 0, chars->Length, true ));
   encoder->GetBytes( chars, 0, chars->Length, allCharactersFromEncoder, 0, true );
   Console::WriteLine( "All characters encoded:" );
   ShowArray( allCharactersFromEncoder );
   
   // Do not flush state; i.e. maintain state between calls.
   bool bFlushState = false;
   
   // Encode characters one-by-one.
   // By maintaining state, the Encoder will not store extra bytes in the output.
   array<Byte>^firstcharNoFlush = gcnew array<Byte>(encoder->GetByteCount( chars, 0, 1, bFlushState ));
   encoder->GetBytes( chars, 0, 1, firstcharNoFlush, 0, bFlushState );
   Console::WriteLine( "First character:" );
   ShowArray( firstcharNoFlush );
   array<Byte>^secondcharNoFlush = gcnew array<Byte>(encoder->GetByteCount( chars, 1, 1, bFlushState ));
   encoder->GetBytes( chars, 1, 1, secondcharNoFlush, 0, bFlushState );
   Console::WriteLine( "Second character:" );
   ShowArray( secondcharNoFlush );
   array<Byte>^thirdcharNoFlush = gcnew array<Byte>(encoder->GetByteCount( chars, 2, 1, bFlushState ));
   encoder->GetBytes( chars, 2, 1, thirdcharNoFlush, 0, bFlushState );
   Console::WriteLine( "Third character:" );
   ShowArray( thirdcharNoFlush );
   
   // Must flush state on last call to GetBytes().
   bFlushState = true;
   array<Byte>^fourthcharNoFlush = gcnew array<Byte>(encoder->GetByteCount( chars, 3, 1, bFlushState ));
   encoder->GetBytes( chars, 3, 1, fourthcharNoFlush, 0, bFlushState );
   Console::WriteLine( "Fourth character:" );
   ShowArray( fourthcharNoFlush );
}

/* This code example produces the following output.

Using Encoding
--------------
All characters encoded:
[43][65][54][65][68][111][119][79][109][65][54][107][45]

First character:
[43][65][54][65][45]

Second character:
[43][65][54][77][45]

Third character:
[43][65][54][89][45]

Fourth character:
[43][65][54][107][45]

Using Encoder
-------------
All characters encoded:
[43][65][54][65][68][111][119][79][109][65][54][107][45]

First character:
[43][65][54]

Second character:
[65][68][111]

Third character:
[119][79][109]

Fourth character:
[65][54][107][45]


*/
using System;
using System.Text;

class EncoderTest {
    public static void Main() {
        // The characters to encode.
        Char[] chars = new Char[] {
            '\u0023', // #
            '\u0025', // %
            '\u03a0', // Pi
            '\u03a3'  // Sigma
        };

        // Encode characters using an Encoding object.
        Encoding encoding = Encoding.UTF7;
        Console.WriteLine("Using Encoding\n--------------");

        // Encode complete array for comparison.
        Byte[] allCharactersFromEncoding = encoding.GetBytes(chars);
        Console.WriteLine("All characters encoded:");
        ShowArray(allCharactersFromEncoding);

        // Encode characters, one-by-one.
        // The Encoding object will NOT maintain state between calls.
        Byte[] firstchar = encoding.GetBytes(chars, 0, 1);
        Console.WriteLine("First character:");
        ShowArray(firstchar);

        Byte[] secondchar = encoding.GetBytes(chars, 1, 1);
        Console.WriteLine("Second character:");
        ShowArray(secondchar);

        Byte[] thirdchar = encoding.GetBytes(chars, 2, 1);
        Console.WriteLine("Third character:");
        ShowArray(thirdchar);

        Byte[] fourthchar = encoding.GetBytes(chars, 3, 1);
        Console.WriteLine("Fourth character:");
        ShowArray(fourthchar);


        // Now, encode characters using an Encoder object.
        Encoder encoder = encoding.GetEncoder();
        Console.WriteLine("Using Encoder\n-------------");

        // Encode complete array for comparison.
        Byte[] allCharactersFromEncoder = new Byte[encoder.GetByteCount(chars, 0, chars.Length, true)];
        encoder.GetBytes(chars, 0, chars.Length, allCharactersFromEncoder, 0, true);
        Console.WriteLine("All characters encoded:");
        ShowArray(allCharactersFromEncoder);

        // Do not flush state; i.e. maintain state between calls.
        bool bFlushState = false;

        // Encode characters one-by-one.
        // By maintaining state, the Encoder will not store extra bytes in the output.
        Byte[] firstcharNoFlush = new Byte[encoder.GetByteCount(chars, 0, 1, bFlushState)];
        encoder.GetBytes(chars, 0, 1, firstcharNoFlush, 0, bFlushState);
        Console.WriteLine("First character:");
        ShowArray(firstcharNoFlush);

        Byte[] secondcharNoFlush = new Byte[encoder.GetByteCount(chars, 1, 1, bFlushState)];
        encoder.GetBytes(chars, 1, 1, secondcharNoFlush, 0, bFlushState);
        Console.WriteLine("Second character:");
        ShowArray(secondcharNoFlush);

        Byte[] thirdcharNoFlush = new Byte[encoder.GetByteCount(chars, 2, 1, bFlushState)];
        encoder.GetBytes(chars, 2, 1, thirdcharNoFlush, 0, bFlushState);
        Console.WriteLine("Third character:");
        ShowArray(thirdcharNoFlush);

        // Must flush state on last call to GetBytes().
        bFlushState = true;
        
        Byte[] fourthcharNoFlush = new Byte[encoder.GetByteCount(chars, 3, 1, bFlushState)];
        encoder.GetBytes(chars, 3, 1, fourthcharNoFlush, 0, bFlushState);
        Console.WriteLine("Fourth character:");
        ShowArray(fourthcharNoFlush);
    }

    public static void ShowArray(Array theArray) {
        foreach (Object o in theArray) {
            Console.Write("[{0}]", o);
        }
        Console.WriteLine("\n");
    }
}

/* This code example produces the following output.

Using Encoding
--------------
All characters encoded:
[43][65][67][77][65][74][81][79][103][65][54][77][45]

First character:
[43][65][67][77][45]

Second character:
[43][65][67][85][45]

Third character:
[43][65][54][65][45]

Fourth character:
[43][65][54][77][45]

Using Encoder
-------------
All characters encoded:
[43][65][67][77][65][74][81][79][103][65][54][77][45]

First character:
[43][65][67]

Second character:
[77][65][74]

Third character:
[81][79][103]

Fourth character:
[65][54][77][45]


*/
Imports System
Imports System.Text
Imports Microsoft.VisualBasic
Imports Microsoft.VisualBasic.Strings

Class EncoderTest
    
    Public Shared Sub Main()
        ' Unicode characters.
        ' ChrW(35)  = #
        ' ChrW(37)  = %
        ' ChrW(928) = Pi
        ' ChrW(931) = Sigma
        Dim chars() As Char = {ChrW(35), ChrW(37), ChrW(928), ChrW(931)}
        
        ' Encode characters using an Encoding object.
        Dim encoding As Encoding = Encoding.UTF7
        Console.WriteLine( _
            "Using Encoding" & _
            ControlChars.NewLine & _
            "--------------" _
        )
        
        ' Encode complete array for comparison.
        Dim allCharactersFromEncoding As Byte() = encoding.GetBytes(chars)
        Console.WriteLine("All characters encoded:")
        ShowArray(allCharactersFromEncoding)
        
        ' Encode characters, one-by-one.
        ' The Encoding object will NOT maintain state between calls.
        Dim firstchar As Byte() = encoding.GetBytes(chars, 0, 1)
        Console.WriteLine("First character:")
        ShowArray(firstchar)
        
        Dim secondchar As Byte() = encoding.GetBytes(chars, 1, 1)
        Console.WriteLine("Second character:")
        ShowArray(secondchar)
        
        Dim thirdchar As Byte() = encoding.GetBytes(chars, 2, 1)
        Console.WriteLine("Third character:")
        ShowArray(thirdchar)
        
        Dim fourthchar As Byte() = encoding.GetBytes(chars, 3, 1)
        Console.WriteLine("Fourth character:")
        ShowArray(fourthchar)
        
        
        ' Now, encode characters using an Encoder object.
        Dim encoder As Encoder = encoding.GetEncoder()
        Console.WriteLine( _
            "Using Encoder" & _
            ControlChars.NewLine & _
            "-------------" _
        )
        
        ' Encode complete array for comparison.
        Dim allCharactersFromEncoder( _
            encoder.GetByteCount(chars, 0, chars.Length, True) _
        ) As Byte
        encoder.GetBytes(chars, 0, chars.Length, allCharactersFromEncoder, 0, True)
        Console.WriteLine("All characters encoded:")
        ShowArray(allCharactersFromEncoder)
        
        ' Do not flush state; i.e. maintain state between calls.
        Dim bFlushState As Boolean = False
        
        ' Encode characters one-by-one.
        ' By maintaining state, the Encoder will not store extra bytes in the output.
        Dim firstcharNoFlush( _
            encoder.GetByteCount(chars, 0, 1, bFlushState) _
        ) As Byte
        encoder.GetBytes(chars, 0, 1, firstcharNoFlush, 0, bFlushState)
        Console.WriteLine("First character:")
        ShowArray(firstcharNoFlush)
        
        Dim secondcharNoFlush( _
            encoder.GetByteCount(chars, 1, 1, bFlushState) _
        ) As Byte
        encoder.GetBytes(chars, 1, 1, secondcharNoFlush, 0, bFlushState)
        Console.WriteLine("Second character:")
        ShowArray(secondcharNoFlush)
        
        Dim thirdcharNoFlush( _
            encoder.GetByteCount(chars, 2, 1, bFlushState) _
        ) As Byte
        encoder.GetBytes(chars, 2, 1, thirdcharNoFlush, 0, bFlushState)
        Console.WriteLine("Third character:")
        ShowArray(thirdcharNoFlush)
        
        ' Must flush state on last call to GetBytes().
        bFlushState = True
        
        Dim fourthcharNoFlush( _
            encoder.GetByteCount(chars, 3, 1, bFlushState) _
        ) As Byte
        encoder.GetBytes(chars, 3, 1, fourthcharNoFlush, 0, bFlushState)
        Console.WriteLine("Fourth character:")
        ShowArray(fourthcharNoFlush)
    End Sub 'Main
    
    
    Public Shared Sub ShowArray(theArray As Array)
        Dim o As Object
        For Each o In  theArray
            Console.Write("[{0}]", o)
        Next o
        Console.WriteLine(ControlChars.NewLine)
    End Sub 'ShowArray
End Class 'EncoderTest

'This code example produces the following output.
'
'Using Encoding
'--------------
'All characters encoded:
'[43][65][67][77][65][74][81][79][103][65][54][77][45]
'
'First character:
'[43][65][67][77][45]
'
'Second character:
'[43][65][67][85][45]
'
'Third character:
'[43][65][54][65][45]
'
'Fourth character:
'[43][65][54][77][45]
'
'Using Encoder
'-------------
'All characters encoded:
'[43][65][67][77][65][74][81][79][103][65][54][77][45][0]
'
'First character:
'[43][65][67][0]
'
'Second character:
'[77][65][74][0]
'
'Third character:
'[81][79][103][0]
'
'Fourth character:
'[65][54][77][45][0]
'

Comentarios

Para obtener una instancia de una implementación de la Encoder (clase), la aplicación debe utilizar el GetEncoder método de un Encoding implementación.To obtain an instance of an implementation of the Encoder class, the application should use the GetEncoder method of an Encoding implementation.

El GetByteCount método determina el número de bytes resultante de codificar un juego de caracteres Unicode y el GetBytes método realiza la codificación real.The GetByteCount method determines how many bytes result in encoding a set of Unicode characters, and the GetBytes method performs the actual encoding. Hay varias versiones de estos dos métodos disponibles en la Encoder clase.There are several versions of both of these methods available in the Encoder class. Para obtener más información, vea Encoding.GetBytes.For more information, see Encoding.GetBytes.

Un Encoder objeto mantiene información de estado entre las llamadas sucesivas a GetBytes o Convert secuencias que abarcan distintos bloques de caracteres de los métodos, de modo que TI puede codificar correctamente.A Encoder object maintains state information between successive calls to GetBytes or Convert methods so that it can correctly encode character sequences that span blocks. El Encoder también conserva finales caracteres al final de los bloques de datos y usa caracteres finales en la siguiente operación de codificación.The Encoder also preserves trailing characters at the end of data blocks and uses the trailing characters in the next encoding operation. Por ejemplo, un bloque de datos podría finalizar con un suplente alto no coincidente, y el suplente bajo podría estar en el siguiente bloque de datos.For example, a data block might end with an unmatched high surrogate, and the matching low surrogate might be in the next data block. Por lo tanto, GetDecoder y GetEncoder son útiles para la transmisión de red y las operaciones de archivo, porque esas operaciones a menudo tratan con bloques de datos en lugar de un flujo de datos completa.Therefore, GetDecoder and GetEncoder are useful for network transmission and file operations, because those operations often deal with blocks of data instead of a complete data stream.

Nota

Cuando la aplicación ha terminado con un flujo de datos debe asegurarse de que se vacíe la información de estado estableciendo el flush parámetro true en la llamada al método apropiado.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. Si se produce una excepción o si la aplicación modifica los flujos, debe llamar a Reset para borrar el estado interno de la Encoder objeto.If an exception occurs or if the application switches streams, it should call Reset to clear the internal state of the Encoder object.

Consideraciones sobre la versiónVersion Considerations

Un Decoder o Encoder se puede serializar el objeto durante una operación de conversión.A Decoder or Encoder object can be serialized during a conversion operation. El estado del objeto se conserva si se ha deserializado en la misma versión de .NET Framework, pero se pierden si se deserializa en otra versión.The state of the object is retained if it is deserialized in the same version of the .NET Framework, but lost if it is deserialized in another version.

Notas a los desarrolladores de herederos

Cuando la aplicación se hereda de esta clase, deben invalidar a todos los miembros.When your application inherits from this class, it must override all the members.

Constructores

Encoder() Encoder() Encoder() Encoder()

Inicializa una nueva instancia de la clase Encoder.Initializes a new instance of the Encoder class.

Propiedades

Fallback Fallback Fallback Fallback

Obtiene o establece un objeto EncoderFallback para el objeto Encoder actual.Gets or sets a EncoderFallback object for the current Encoder object.

FallbackBuffer FallbackBuffer FallbackBuffer FallbackBuffer

Obtiene el objeto EncoderFallbackBuffer asociado al objeto Encoder actual.Gets the EncoderFallbackBuffer object associated with the current Encoder object.

Métodos

Convert(Char*, Int32, Byte*, Int32, Boolean, Int32, Int32, Boolean) Convert(Char*, Int32, Byte*, Int32, Boolean, Int32, Int32, Boolean) Convert(Char*, Int32, Byte*, Int32, Boolean, Int32, Int32, Boolean) 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) Convert(Char[], Int32, Int32, Byte[], Int32, Int32, Boolean, Int32, Int32, Boolean) Convert(Char[], Int32, Int32, Byte[], Int32, Int32, Boolean, Int32, Int32, Boolean) 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) Convert(ReadOnlySpan<Char>, Span<Byte>, Boolean, Int32, Int32, Boolean) Convert(ReadOnlySpan<Char>, Span<Byte>, Boolean, Int32, Int32, Boolean) Convert(ReadOnlySpan<Char>, Span<Byte>, Boolean, Int32, Int32, Boolean)
Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determina si el objeto especificado es igual al objeto actual.Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetByteCount(Char*, Int32, Boolean) GetByteCount(Char*, Int32, Boolean) GetByteCount(Char*, Int32, Boolean) GetByteCount(Char*, Int32, Boolean)

Cuando se reemplaza en una clase derivada, calcula el número de bytes que se generan al codificar un juego de caracteres a partir del puntero de caracteres especificado.When overridden in a derived class, calculates the number of bytes produced by encoding a set of characters starting at the specified character pointer. Un parámetro indica si se debe borrar el estado interno del codificador después del cálculo.A parameter indicates whether to clear the internal state of the encoder after the calculation.

GetByteCount(Char[], Int32, Int32, Boolean) GetByteCount(Char[], Int32, Int32, Boolean) GetByteCount(Char[], Int32, Int32, Boolean) GetByteCount(Char[], Int32, Int32, Boolean)

Cuando se reemplaza en una clase derivada, calcula el número de bytes que se generan al codificar un juego de caracteres de la matriz de caracteres especificada.When overridden in a derived class, calculates the number of bytes produced by encoding a set of characters from the specified character array. Un parámetro indica si se debe borrar el estado interno del codificador después del cálculo.A parameter indicates whether to clear the internal state of the encoder after the calculation.

GetByteCount(ReadOnlySpan<Char>, Boolean) GetByteCount(ReadOnlySpan<Char>, Boolean) GetByteCount(ReadOnlySpan<Char>, Boolean) GetByteCount(ReadOnlySpan<Char>, Boolean)
GetBytes(Char*, Int32, Byte*, Int32, Boolean) GetBytes(Char*, Int32, Byte*, Int32, Boolean) GetBytes(Char*, Int32, Byte*, Int32, Boolean) GetBytes(Char*, Int32, Byte*, Int32, Boolean)

Cuando se reemplaza en una clase derivada, codifica un juego de caracteres a partir del puntero de carácter especificado y cualquier carácter del búfer interno en una secuencia de bytes que se almacenan a partir del puntero de byte especificado.When overridden in a derived class, encodes a set of characters starting at the specified character pointer and any characters in the internal buffer into a sequence of bytes that are stored starting at the specified byte pointer. Un parámetro indica si se debe borrar el estado interno del codificador después de la conversión.A parameter indicates whether to clear the internal state of the encoder after the conversion.

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

Cuando se reemplaza en una clase derivada, codifica un juego de caracteres de la matriz de caracteres especificada y cualquier carácter del búfer interno en la matriz de bytes especificada.When overridden in a derived class, encodes a set of characters from the specified character array and any characters in the internal buffer into the specified byte array. Un parámetro indica si se debe borrar el estado interno del codificador después de la conversión.A parameter indicates whether to clear the internal state of the encoder after the conversion.

GetBytes(ReadOnlySpan<Char>, Span<Byte>, Boolean) GetBytes(ReadOnlySpan<Char>, Span<Byte>, Boolean) GetBytes(ReadOnlySpan<Char>, Span<Byte>, Boolean) GetBytes(ReadOnlySpan<Char>, Span<Byte>, Boolean)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Sirve como la función hash predeterminada.Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

Obtiene el Type de la instancia actual.Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Crea una copia superficial del objeto Object actual.Creates a shallow copy of the current Object.

(Inherited from Object)
Reset() Reset() Reset() Reset()

Cuando se reemplaza en una clase derivada, restablece el estado inicial del codificador.When overridden in a derived class, sets the encoder back to its initial state.

ToString() ToString() ToString() ToString()

Devuelve una cadena que representa el objeto actual.Returns a string that represents the current object.

(Inherited from Object)

Se aplica a

Consulte también: