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

Definicja

Dekoduje zakres bajtów z tablicy bajtów do ciągu.

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

Parametry

bytes
Byte[]

Tablica bajtów zawierająca sekwencję bajtów do dekodowania.

index
Int32

Indeks pierwszego bajtu do dekodowania.

count
Int32

Liczba bajtów do dekodowania.

Zwraca

String

Element String zawierający wyniki dekodowania określonej sekwencji bajtów.

Atrybuty

Wyjątki

bytes is null (Nothing).

index wartość lub count jest mniejsza niż zero.

-lub- index i count nie oznaczają prawidłowego zakresu w byteselemecie .

Wystąpił rezerwowy (aby uzyskać więcej informacji, zobacz Kodowanie znaków na platformie .NET).

-and- DecoderFallback jest ustawiona na DecoderExceptionFallbackwartość .

Przykłady

Poniższy przykład kodu koduje ciąg do tablicy bajtów, a następnie dekoduje bajty z powrotem do ciągu.

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

Uwagi

Dane, które mają być konwertowane, takie jak dane odczytywane ze strumienia, mogą być dostępne tylko w blokach sekwencyjnych. W takim przypadku lub jeśli ilość danych jest tak duża, że musi być podzielona na mniejsze bloki, aplikacja powinna używać Decoder wartości lub Encoder dostarczonej GetDecoder odpowiednio przez metodę lub GetEncoder metodę.

Uwaga

UTF7Encoding nie zapewnia wykrywania błędów. W przypadku napotkania UTF7Encoding nieprawidłowych bajtów zazwyczaj emituje nieprawidłowe bajty. Jeśli bajt jest większy niż szesnastkowy 0x7F, wartość bajtu jest od zera rozszerzona na znak Unicode, wynik jest przechowywany w chars tablicy, a każda sekwencja przesunięcia zostanie zakończona. Jeśli na przykład bajt do kodowania jest 0x81 szesnastkowy, wynikowy znak to U+0081. Ze względów bezpieczeństwa aplikacje są zalecane do używania UTF8Encodingfunkcji , UnicodeEncodinglub UTF32Encoding i włączania wykrywania błędów.

Dotyczy

Zobacz też