UnicodeEncoding.GetString(Byte[], Int32, Int32) Metod

Definition

Avkodar ett intervall med byte från en bytematris till en sträng.

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

Parametrar

bytes
Byte[]

Bytematrisen som innehåller sekvensen med byte som ska avkodas.

index
Int32

Indexet för den första byte som ska avkodas.

count
Int32

Antalet byte som ska avkodas.

Returer

Ett String objekt som innehåller resultatet av avkodningen av den angivna sekvensen med byte.

Attribut

Undantag

bytes är null (Nothing).

index eller count är mindre än noll.

-eller-

index och count ange inte ett giltigt intervall i bytes.

Felidentifiering är aktiverat och bytes innehåller en ogiltig sekvens med byte.

En reserv inträffade (mer information finns i Character Encoding i .NET)

-och-

DecoderFallback är inställt på DecoderExceptionFallback.

Exempel

I följande exempel initieras en matris genom att anropa GetByteCount metoden för att fastställa exakt hur många byte som krävs för en kodad sträng och sedan lägga till storleken på byteordningsmärket (BOM). I exemplet anropas GetPreamble sedan metoden för att lagra bommen i matrisen innan metoden anropas GetBytes för att lagra de kodade bytea i matrisen. Exemplet anropar GetString sedan metoden för att avkoda strängen.

using System;
using System.Text;

public class Example
{
   public static void Main()
   {
      UTF8Encoding utf8 = new UTF8Encoding(true, true);

      String s = "It was the best of times, it was the worst of times...";

      // We need to dimension the array, since we'll populate it with 2 method calls.
      Byte[] bytes = new Byte[utf8.GetByteCount(s) + utf8.GetPreamble().Length];
      // Encode the string.
      Array.Copy(utf8.GetPreamble(), bytes, utf8.GetPreamble().Length);
      utf8.GetBytes(s, 0, s.Length, bytes, utf8.GetPreamble().Length);

      // Decode the byte array.
      String s2 = utf8.GetString(bytes, 0, bytes.Length);
      Console.WriteLine(s2);
   }
}
// The example displays the following output:
//        ?It was the best of times, it was the worst of times...
Imports System.Text

Module Example
   Public Sub Main()
      Dim utf8 As New UTF8Encoding(True, True)

      Dim s As String = "It was the best of times, it was the worst of times..."

      ' We need to dimension the array, since we'll populate it with 2 method calls.
      Dim bytes(utf8.GetByteCount(s) + utf8.GetPreamble().Length - 1) As Byte
      ' Encode the string.
      Array.Copy(utf8.GetPreamble(), bytes, utf8.GetPreamble().Length)
      utf8.GetBytes(s, 0, s.Length, bytes, utf8.GetPreamble().Length)

      ' Decode the byte array.
      Dim s2 As String = utf8.GetString(bytes, 0, bytes.Length)
      Console.WriteLine(s2)
   End Sub
End Module
' The example displays the following output:
'       ?It was the best of times, it was the worst of times...

Observera att i det här fallet skiljer sig den avkodade strängen från den ursprungliga strängen, eftersom den börjar med en 16-bitars byteordningsmarkering U+FFFD. Det innebär att de två strängarna jämförs som ojämlika, och om strängen är utdata visas strukturlistan som ersättningstecknet "?". Om du vill ta bort bommen i början av strängen kan du anropa String.TrimStart metoden.

Kommentarer

Med felidentifiering orsakar en ogiltig sekvens att den här metoden genererar en ArgumentException. Utan felidentifiering ignoreras ogiltiga sekvenser och inget undantag utlöses.

Om byteintervallet som ska avkodas innehåller byteordningsmarkeringen (BOM) och bytematrisen returnerades av en metod av en icke-BOM-medveten typ inkluderas tecknet U+FFFE i teckenmatrisen som returneras av den här metoden. Du kan ta bort den genom att String.TrimStart anropa metoden.

Data som ska konverteras, till exempel data som lästs från en dataström, kan endast vara tillgängliga i sekventiella block. I det här fallet, eller om mängden data är så stor att den måste delas upp i mindre block, bör programmet använda Decoder eller - Encoder objektet som tillhandahålls av GetDecoder metoden eller GetEncoder .

Gäller för

Se även