Encoding.Convert Encoding.Convert Encoding.Convert Encoding.Convert Method

Definición

Convierte una matriz de bytes de una codificación a otra.Converts a byte array from one encoding to another.

Sobrecargas

Convert(Encoding, Encoding, Byte[], Int32, Int32) Convert(Encoding, Encoding, Byte[], Int32, Int32) Convert(Encoding, Encoding, Byte[], Int32, Int32) Convert(Encoding, Encoding, Byte[], Int32, Int32)

Convierte un intervalo de bytes de una matriz de una codificación a otra.Converts a range of bytes in a byte array from one encoding to another.

Convert(Encoding, Encoding, Byte[]) Convert(Encoding, Encoding, Byte[]) Convert(Encoding, Encoding, Byte[]) Convert(Encoding, Encoding, Byte[])

Convierte una matriz de bytes completa de una codificación a otra.Converts an entire byte array from one encoding to another.

Convert(Encoding, Encoding, Byte[], Int32, Int32) Convert(Encoding, Encoding, Byte[], Int32, Int32) Convert(Encoding, Encoding, Byte[], Int32, Int32) Convert(Encoding, Encoding, Byte[], Int32, Int32)

Convierte un intervalo de bytes de una matriz de una codificación a otra.Converts a range of bytes in a byte array from one encoding to another.

public:
 static cli::array <System::Byte> ^ Convert(System::Text::Encoding ^ srcEncoding, System::Text::Encoding ^ dstEncoding, cli::array <System::Byte> ^ bytes, int index, int count);
public static byte[] Convert (System.Text.Encoding srcEncoding, System.Text.Encoding dstEncoding, byte[] bytes, int index, int count);
static member Convert : System.Text.Encoding * System.Text.Encoding * byte[] * int * int -> byte[]
Public Shared Function Convert (srcEncoding As Encoding, dstEncoding As Encoding, bytes As Byte(), index As Integer, count As Integer) As Byte()

Parámetros

srcEncoding
Encoding Encoding Encoding Encoding

Codificación de la matriz de origen, bytes.The encoding of the source array, bytes.

dstEncoding
Encoding Encoding Encoding Encoding

Codificación de la matriz de salida.The encoding of the output array.

bytes
Byte[]

Matriz de bytes que se va a convertir.The array of bytes to convert.

index
Int32 Int32 Int32 Int32

Índice del primer elemento de bytes que se va a convertir.The index of the first element of bytes to convert.

count
Int32 Int32 Int32 Int32

Número de bytes que se va a convertir.The number of bytes to convert.

Devoluciones

Byte[]

Matriz de tipo Byte que contiene el resultado de convertir un intervalo de bytes de bytes de srcEncoding a dstEncoding.An array of type Byte containing the result of converting a range of bytes in bytes from srcEncoding to dstEncoding.

Excepciones

srcEncoding es null.srcEncoding is null.

O bien-or- dstEncoding es null.dstEncoding is null.

O bien-or- bytes es null.bytes is null.

index y count no especifican un intervalo válido en la matriz de bytes.index and count do not specify a valid range in the byte array.

Se ha producido una acción de reserva (vea Codificación de caracteres de .NET para obtener una explicación completa)A fallback occurred (see Character Encoding in .NET for complete explanation) - y --and- srcEncoding.srcEncoding. El valor de DecoderFallback está establecido en DecoderExceptionFallback.DecoderFallback is set to DecoderExceptionFallback.

Se ha producido una acción de reserva (vea Codificación de caracteres de .NET para obtener una explicación completa)A fallback occurred (see Character Encoding in .NET for complete explanation) - y --and- dstEncoding.dstEncoding. El valor de EncoderFallback está establecido en EncoderExceptionFallback.EncoderFallback is set to EncoderExceptionFallback.

Convert(Encoding, Encoding, Byte[]) Convert(Encoding, Encoding, Byte[]) Convert(Encoding, Encoding, Byte[]) Convert(Encoding, Encoding, Byte[])

Convierte una matriz de bytes completa de una codificación a otra.Converts an entire byte array from one encoding to another.

public:
 static cli::array <System::Byte> ^ Convert(System::Text::Encoding ^ srcEncoding, System::Text::Encoding ^ dstEncoding, cli::array <System::Byte> ^ bytes);
public static byte[] Convert (System.Text.Encoding srcEncoding, System.Text.Encoding dstEncoding, byte[] bytes);
static member Convert : System.Text.Encoding * System.Text.Encoding * byte[] -> byte[]
Public Shared Function Convert (srcEncoding As Encoding, dstEncoding As Encoding, bytes As Byte()) As Byte()

Parámetros

srcEncoding
Encoding Encoding Encoding Encoding

Formato de codificación de bytes.The encoding format of bytes.

dstEncoding
Encoding Encoding Encoding Encoding

Formato de codificación de destino.The target encoding format.

bytes
Byte[]

Bytes que se van a convertir.The bytes to convert.

Devoluciones

Byte[]

Matriz de tipo Byte que contiene el resultado de convertir bytes de srcEncoding a dstEncoding.An array of type Byte containing the results of converting bytes from srcEncoding to dstEncoding.

Excepciones

srcEncoding es null.srcEncoding is null.

O bien-or- dstEncoding es null.dstEncoding is null.

O bien-or- bytes es null.bytes is null.

Se ha producido una acción de reserva (vea Codificación de caracteres de .NET para obtener una explicación completa)A fallback occurred (see Character Encoding in .NET for complete explanation) - y --and- srcEncoding.srcEncoding. El valor de DecoderFallback está establecido en DecoderExceptionFallback.DecoderFallback is set to DecoderExceptionFallback.

Se ha producido una acción de reserva (vea Codificación de caracteres de .NET para obtener una explicación completa)A fallback occurred (see Character Encoding in .NET for complete explanation) - y --and- dstEncoding.dstEncoding. El valor de EncoderFallback está establecido en EncoderExceptionFallback.EncoderFallback is set to EncoderExceptionFallback.

Ejemplos

El ejemplo siguiente convierte una cadena con codificación Unicode en una cadena codificada en ASCII.The following example converts a Unicode-encoded string to an ASCII-encoded string. Dado que el objeto de codificación ASCII devuelto por la ASCII propiedad usa la reserva de reemplazo y el carácter de Pi no forma parte del juego de caracteres ASCII, el carácter de Pi se reemplaza por un signo de interrogación, como la salida se muestra en el ejemplo.Because the ASCII encoding object returned by the ASCII property uses replacement fallback and the Pi character is not part of the ASCII character set, the Pi character is replaced with a question mark, as the output from the example shows.

using namespace System;
using namespace System::Text;

int 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 array.
   array<Byte>^unicodeBytes = unicode->GetBytes( unicodeString );
   
   // Perform the conversion from one encoding to the other.
   array<Byte>^asciiBytes = Encoding::Convert( unicode, ascii, unicodeBytes );
   
   // Convert the new Byte into[] a char and[] then into a string.
   array<Char>^asciiChars = gcnew array<Char>(ascii->GetCharCount( asciiBytes, 0, asciiBytes->Length ));
   ascii->GetChars( asciiBytes, 0, asciiBytes->Length, asciiChars, 0 );
   String^ asciiString = gcnew String( asciiChars );
   
   // Display the strings created before and after the conversion.
   Console::WriteLine( "Original String*: {0}", unicodeString );
   Console::WriteLine( "Ascii converted String*: {0}", asciiString );
}
// The example displays the following output:
//    Original string: This string contains the unicode character Pi (Π)
//    Ascii converted string: This string contains the unicode character Pi (?)
using System;
using System.Text;

class Example
{
   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 array.
      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.
      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);
   }
}
// The example displays the following output:
//    Original string: This string contains the unicode character Pi (Π)
//    Ascii converted string: This string contains the unicode character Pi (?)
Imports System.Text

Class Example
   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 array.
      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 array into a char array and then into a string.
      Dim asciiChars(ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)-1) 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
' The example displays the following output:
'    Original string: This string contains the unicode character Pi (Π)
'    Ascii converted string: This string contains the unicode character Pi (?)

Se aplica a