UTF7Encoding.GetString(Byte[], Int32, Int32) Metodo

Definizione

Decodifica un intervallo di byte da una matrice di byte in una stringa.

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

Parametri

bytes
Byte[]

Matrice di byte contenente la sequenza di byte da decodificare.

index
Int32

Indice del primo byte da decodificare.

count
Int32

Numero di byte da decodificare.

Restituisce

Oggetto String contenente i risultati di decodifica della sequenza di byte specificata.

Attributi

Eccezioni

bytes è null (Nothing).

index o count è minore di zero.

-oppure-

index e count non indicano un intervallo valido in bytes.

Si è verificato un fallback (per altre informazioni, vedere Codifica dei caratteri in .NET).

-e-

DecoderFallback è impostato su DecoderExceptionFallback.

Esempio

Nell'esempio di codice seguente viene codificata una stringa in una matrice di byte e quindi decodifica i byte in una stringa.

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

Commenti

I dati da convertire, ad esempio i dati letti da un flusso, potrebbero essere disponibili solo in blocchi sequenziali. In questo caso, o se la quantità di dati è così grande che deve essere suddivisa in blocchi più piccoli, l'applicazione deve usare DecoderEncoder rispettivamente o il GetDecoder metodo fornito dal metodo o dal GetEncoder metodo.

Nota

UTF7Encoding non fornisce il rilevamento degli errori. Quando vengono rilevati byte non validi, UTF7Encoding in genere genera i byte non validi. Se un byte è maggiore dell'esadecimale 0x7F, il valore di byte viene esteso zero in un carattere Unicode, il risultato viene archiviato nella chars matrice e qualsiasi sequenza di maiuscole viene terminata. Ad esempio, se il byte da codificare è esadecimale 0x81, il carattere risultante è U+0081. Per motivi di sicurezza, è consigliabile usare UTF8Encoding, UnicodeEncodingo UTF32Encoding e abilitare il rilevamento degli errori.

Si applica a

Vedi anche