UTF7Encoding.GetString(Byte[], Int32, Int32) Método

Definición

Descodifica un intervalo de bytes de una matriz de bytes en una cadena.

public:
 override System::String ^ GetString(cli::array <System::Byte> ^ bytes, int index, int count);
public override string GetString (byte[] bytes, int index, int count);
[System.Runtime.InteropServices.ComVisible(false)]
public override string GetString (byte[] bytes, int index, int count);
override this.GetString : byte[] * int * int -> string
[<System.Runtime.InteropServices.ComVisible(false)>]
override this.GetString : byte[] * int * int -> string
Public Overrides Function GetString (bytes As Byte(), index As Integer, count As Integer) As String

Parámetros

bytes
Byte[]

Matriz de bytes que contiene la secuencia de bytes que se va a descodificar.

index
Int32

Índice del primer byte que se va a descodificar.

count
Int32

Número de bytes que se van a descodificar.

Devoluciones

Objeto String que contiene los resultados obtenidos al descodificar la secuencia de bytes especificada.

Atributos

Excepciones

bytes es null (Nothing).

index o count es menor que cero.

O bien

index y count no denotan un intervalo válido en bytes.

Se ha producido una reserva (para obtener más información, vea Codificación de caracteres en .NET).

- y -

El valor de DecoderFallback está establecido en DecoderExceptionFallback.

Ejemplos

En el ejemplo de código siguiente se codifica una cadena en una matriz de bytes y, a continuación, se descodifican los bytes en una cadena.

using namespace System;
using namespace System::Text;
int main()
{
   
   // Create an instance of UTF7Encoding.
   UTF7Encoding^ u7 = gcnew UTF7Encoding( true );
   
   // Create byte arrays from the same string containing the following characters:
   //    Latin Small Letter Z (U+007A)
   //    Latin Small Letter A (U+0061)
   //    Combining Breve (U+0306)
   //    Latin Small Letter AE With Acute (U+01FD)
   //    Greek Small Letter Beta (U+03B2)
   String^ myStr = "za\u0306\u01FD\u03B2";
   
   // Encode the string.
   array<Byte>^myBArr = gcnew array<Byte>(u7->GetByteCount( myStr ));
   u7->GetBytes( myStr, 0, myStr->Length, myBArr, 0 );
   
   // Decode the byte array.
   Console::WriteLine( "The new string is: {0}", u7->GetString( myBArr, 0, myBArr->Length ) );
}

/*
This code produces the following output.  The question marks take the place of characters that cannot be displayed at the console.

The new string is: za??

*/
using System;
using System.Text;

public class SamplesUTF7Encoding  {

   public static void Main()  {

      // Create an instance of UTF7Encoding.
      UTF7Encoding u7 = new UTF7Encoding( true );

      // Create byte arrays from the same string containing the following characters:
      //    Latin Small Letter Z (U+007A)
      //    Latin Small Letter A (U+0061)
      //    Combining Breve (U+0306)
      //    Latin Small Letter AE With Acute (U+01FD)
      //    Greek Small Letter Beta (U+03B2)
      String myStr = "za\u0306\u01FD\u03B2";

      // Encode the string.
      byte[] myBArr = new byte[u7.GetByteCount( myStr )];
      u7.GetBytes( myStr, 0, myStr.Length, myBArr, 0 );

      // Decode the byte array.
      Console.WriteLine( "The new string is: {0}", u7.GetString( myBArr, 0, myBArr.Length ) );
   }
}


/*
This code produces the following output.  The question marks take the place of characters that cannot be displayed at the console.

The new string is: za??

*/
Imports System.Text

Public Class SamplesUTF7Encoding

   Public Shared Sub Main()

      ' Create an instance of UTF7Encoding.
      Dim u7 As New UTF7Encoding(True)

      ' Create byte arrays from the same string containing the following characters:
      '    Latin Small Letter Z (U+007A)
      '    Latin Small Letter A (U+0061)
      '    Combining Breve (U+0306)
      '    Latin Small Letter AE With Acute (U+01FD)
      '    Greek Small Letter Beta (U+03B2)
      Dim myStr As String = "za" & ChrW(&H0306) & ChrW(&H01FD) & ChrW(&H03B2)

      ' Encode the string.
      Dim myBArr(u7.GetByteCount(myStr)) As Byte
      u7.GetBytes(myStr, 0, myStr.Length, myBArr, 0)

      ' Decode the byte array.
      Console.WriteLine("The new string is: {0}", u7.GetString(myBArr, 0, myBArr.Length))

   End Sub

End Class


'This code produces the following output.  The question marks take the place of characters that cannot be displayed at the console.
'
'The new string is: za??ß

Comentarios

Los datos que se van a convertir, como los datos leídos de una secuencia, pueden estar disponibles solo en bloques secuenciales. En este caso, o si la cantidad de datos es tan grande que debe dividirse en bloques más pequeños, la aplicación debe usar o Decoder el Encoder proporcionado por el GetDecoder método o el GetEncoder método, respectivamente.

Nota:

UTF7Encoding no proporciona detección de errores. Cuando se encuentran bytes no válidos, UTF7Encoding generalmente emite los bytes no válidos. Si un byte es mayor que 0x7F hexadecimal, el valor de byte se extiende cero en un carácter Unicode, el resultado se almacena en la chars matriz y se finaliza cualquier secuencia de desplazamiento. Por ejemplo, si el byte que se va a codificar es 0x81 hexadecimal, el carácter resultante es U+0081. Por motivos de seguridad, se recomienda que las aplicaciones usen UTF8Encoding, UnicodeEncodingo y UTF32Encoding habiliten la detección de errores.

Se aplica a

Consulte también