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

Definição

Decodifica um intervalo de bytes de uma matriz de bytes em uma cadeia de caracteres.Decodes a range of bytes from a byte array into a string.

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[]

A matriz de bytes que contém a sequência de bytes a ser decodificada.The byte array containing the sequence of bytes to decode.

index
Int32

O índice do primeiro byte a ser decodificado.The index of the first byte to decode.

count
Int32

O número de bytes a serem decodificados.The number of bytes to decode.

Retornos

String

Um String que contém os resultados da decodificação da sequência de bytes especificada.A String containing the results of decoding the specified sequence of bytes.

Atributos

Exceções

bytes é null (Nothing).bytes is null (Nothing).

index ou count é menor que zero.index or count is less than zero.

- ou --or- index e count não denotam um intervalo válido em bytes.index and count do not denote a valid range in bytes.

Ocorreu um fallback (confira Codificação de caracteres no .NET para obter uma explicação mais completa).A fallback occurred (see Character Encoding in .NET for fuller explanation).

-e--and- DecoderFallback é definido como DecoderExceptionFallback.DecoderFallback is set to DecoderExceptionFallback.

Exemplos

O exemplo de código a seguir codifica uma cadeia de caracteres em uma matriz de bytes e, em seguida, decodifica os bytes de volta em uma cadeia de caracteres.The following code example encodes a string into an array of bytes, and then decodes the bytes back into a string.

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??ß


Comentários

Os dados a serem convertidos, como dados lidos de um fluxo, podem estar disponíveis somente em blocos sequenciais.Data to be converted, such as data read from a stream, might be available only in sequential blocks. Nesse caso, ou se a quantidade de dados for tão grande que precisa ser dividida em blocos menores, o aplicativo deverá usar o Decoder ou o Encoder fornecido pelo GetDecoder método ou pelo GetEncoder método, respectivamente.In this case, or if the amount of data is so large that it needs to be divided into smaller blocks, the application should use the Decoder or the Encoder provided by the GetDecoder method or the GetEncoder method, respectively.

Observação

UTF7Encoding não fornece detecção de erro.UTF7Encoding does not provide error detection. Quando bytes inválidos são encontrados, UTF7Encoding o geralmente emite bytes inválidos.When invalid bytes are encountered, UTF7Encoding generally emits the invalid bytes. Se um byte for maior que hexadecimal 0x7F, o valor de byte será estendido em um caractere Unicode, o resultado será armazenado na chars matriz e qualquer sequência de deslocamento será encerrada.If a byte is larger than hexadecimal 0x7F, the byte value is zero-extended into a Unicode character, the result is stored in the chars array, and any shift sequence is terminated. Por exemplo, se o byte a ser codificado for hexadecimal 0x81, o caractere resultante será U + 0081.For example, if the byte to encode is hexadecimal 0x81, the resulting character is U+0081. Por motivos de segurança, os aplicativos são recomendados para usar UTF8Encoding , UnicodeEncoding ou UTF32Encoding e habilitar a detecção de erros.For security reasons, your applications are recommended to use UTF8Encoding, UnicodeEncoding, or UTF32Encoding and enable error detection.

Aplica-se a

Confira também