UTF32Encoding.GetString(Byte[], Int32, Int32) Metod
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
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);
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
En sträng som innehåller resultatet av avkodning av den angivna sekvensen med byte.
Undantag
bytes är null.
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 återställning inträffade (mer information finns i Character Encoding i .NET för en fullständig förklaring).
-och-
DecoderFallback är inställt på DecoderExceptionFallback.
Exempel
I följande exempel kodas en sträng i två matriser med byte, en i lite endiansk ordning och den andra i storslutsordning. Sedan avkodas byteen tillbaka till en sträng.
using System;
using System.Text;
public class Example
{
public static void Main()
{
// Create two instances of UTF32Encoding: one with little-endian byte order and one with big-endian byte order.
UTF32Encoding u32LE = new UTF32Encoding(false, true, true);
UTF32Encoding u32BE = new UTF32Encoding(true, true, 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 str = "za\u0306\u01FD\u03B2";
// barrBE uses the big-endian byte order.
byte[] barrBE = new byte[u32BE.GetByteCount(str)];
u32BE.GetBytes(str, 0, str.Length, barrBE, 0);
// barrLE uses the little-endian byte order.
byte[] barrLE = new byte[u32LE.GetByteCount(str)];
u32LE.GetBytes(str, 0, str.Length, barrLE, 0);
// Decode the byte arrays.
Console.WriteLine("BE array with BE encoding:");
DisplayString(barrBE, u32BE);
Console.WriteLine();
Console.WriteLine("LE array with LE encoding:");
DisplayString(barrLE, u32LE);
Console.WriteLine();
// Decode the byte arrays using an encoding with a different byte order.
Console.WriteLine("BE array with LE encoding:");
try {
DisplayString(barrBE, u32LE);
}
catch (System.ArgumentException e) {
Console.WriteLine(e.Message);
}
Console.WriteLine();
Console.WriteLine("LE array with BE encoding:");
try {
DisplayString(barrLE, u32BE);
}
catch (ArgumentException e) {
Console.WriteLine(e.Message);
}
Console.WriteLine();
}
public static void DisplayString(byte[] bytes, Encoding enc)
{
// Display the name of the encoding used.
Console.Write("{0,-25}: ", enc.ToString());
// Decode the bytes and display the characters.
Console.WriteLine(enc.GetString(bytes, 0, bytes.Length));
}
}
// This example displays the following output:
// BE array with BE encoding:
// System.Text.UTF32Encoding: zăǽβ
//
// LE array with LE encoding:
// System.Text.UTF32Encoding: zăǽβ
//
// BE array with LE encoding:
// System.Text.UTF32Encoding: Unable to translate bytes [00][00][00][7A] at index 0 from specified code page to Unicode.
//
// LE array with BE encoding:
// System.Text.UTF32Encoding: Unable to translate bytes [7A][00][00][00] at index 0 from specified code page to Unicode.
Imports System.Text
Public Module Example
Public Sub Main()
' Create two instances of UTF32Encoding: one with little-endian byte order and one with big-endian byte order.
Dim u32LE As New UTF32Encoding(False, True, True)
Dim u32BE As New UTF32Encoding(True, True, 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 str As String = "za" + ChrW(&h0306) + ChrW(&h01FD) + ChrW(&h03B2)
' barrBE uses the big-endian byte order.
Dim barrBE(u32BE.GetByteCount(str) - 1) As Byte
u32BE.GetBytes(str, 0, str.Length, barrBE, 0)
' barrLE uses the little-endian byte order.
Dim barrLE(u32LE.GetByteCount(str) - 1) As Byte
u32LE.GetBytes(str, 0, str.Length, barrLE, 0)
' Decode the byte arrays.
Console.WriteLine("BE array with BE encoding:")
DisplayString(barrBE, u32BE)
Console.WriteLine()
Console.WriteLine("LE array with LE encoding:")
DisplayString(barrLE, u32LE)
Console.WriteLine()
' Decode the byte arrays using an encoding with a different byte order.
Console.WriteLine("BE array with LE encoding:")
Try
DisplayString(barrBE, u32LE)
Catch e As ArgumentException
Console.WriteLine(e.Message)
End Try
Console.WriteLine()
Console.WriteLine("LE array with BE encoding:")
Try
DisplayString(barrLE, u32BE)
Catch e As ArgumentException
Console.WriteLine(e.Message)
End Try
Console.WriteLine()
End Sub
Public Sub DisplayString(bytes As Byte(), enc As Encoding )
' Display the name of the encoding used.
Console.Write("{0,-25}: ", enc.ToString())
' Decode the bytes and display the characters.
Console.WriteLine(enc.GetString(bytes, 0, bytes.Length))
End Sub
End Module
' This example displays the following output:
' BE array with BE encoding:
' System.Text.UTF32Encoding: zăǽβ
'
' LE array with LE encoding:
' System.Text.UTF32Encoding: zăǽβ
'
' BE array with LE encoding:
' System.Text.UTF32Encoding: Unable to translate bytes [00][00][00][7A] at index 0 from specified code page to Unicode.
'
' LE array with BE encoding:
' System.Text.UTF32Encoding: Unable to translate bytes [7A][00][00][00] at index 0 from specified code page to Unicode.
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()
{
var utf32 = new UTF32Encoding(! BitConverter.IsLittleEndian, 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[utf32.GetByteCount(s) + utf32.GetPreamble().Length];
// Encode the string.
Array.Copy(utf32.GetPreamble(), bytes, utf32.GetPreamble().Length);
utf32.GetBytes(s, 0, s.Length, bytes, utf32.GetPreamble().Length);
// Decode the byte array.
String s2 = utf32.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 utf32 As New UTF32Encoding(Not BitConverter.IsLittleEndian, 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(utf32.GetByteCount(s) + utf32.GetPreamble().Length - 1) As Byte
' Encode the string.
Array.Copy(utf32.GetPreamble(), bytes, utf32.GetPreamble().Length)
utf32.GetBytes(s, 0, s.Length, bytes, utf32.GetPreamble().Length)
' Decode the byte array.
Dim s2 As String = utf32.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 32-bitars byteordningsmarkering U+FFFE U+0000. 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 "?".
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 med 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 som Encoder tillhandahålls av GetDecoder metoden eller GetEncoder metoden.