UTF32Encoding.GetChars Methode
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
Decodiert eine Bytefolge in Zeichen.
Überlädt
GetChars(Byte*, Int32, Char*, Int32) |
Decodiert eine Bytefolge beginnend am angegebenen Bytezeiger in Zeichen, die beginnend am angegebenen Zeichenzeiger gespeichert werden. |
GetChars(Byte[], Int32, Int32, Char[], Int32) |
Decodiert eine Bytefolge aus dem angegebenen Bytearray in das angegebene Zeichenarray. |
GetChars(Byte*, Int32, Char*, Int32)
Wichtig
Diese API ist nicht CLS-kompatibel.
Decodiert eine Bytefolge beginnend am angegebenen Bytezeiger in Zeichen, die beginnend am angegebenen Zeichenzeiger gespeichert werden.
public:
override int GetChars(System::Byte* bytes, int byteCount, char* chars, int charCount);
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
public override int GetChars (byte* bytes, int byteCount, char* chars, int charCount);
[System.CLSCompliant(false)]
public override int GetChars (byte* bytes, int byteCount, char* chars, int charCount);
[<System.CLSCompliant(false)>]
[<System.Security.SecurityCritical>]
override this.GetChars : nativeptr<byte> * int * nativeptr<char> * int -> int
[<System.CLSCompliant(false)>]
override this.GetChars : nativeptr<byte> * int * nativeptr<char> * int -> int
Parameter
- bytes
- Byte*
Ein Zeiger auf das erste zu decodierende Byte.
- byteCount
- Int32
Die Anzahl der zu decodierenden Bytes.
- chars
- Char*
Ein Zeiger auf die Position, an der mit dem Schreiben der sich ergebenden Zeichen begonnen werden soll.
- charCount
- Int32
Die maximale Anzahl der zu schreibenden Zeichen.
Gibt zurück
Die durch chars
angegebene tatsächliche Anzahl der Zeichen, die am Speicherort geschrieben wurden.
- Attribute
Ausnahmen
byteCount
oder charCount
ist kleiner als 0.
Die Fehlererkennung ist aktiviert, und bytes
enthält eine ungültige Folge von Bytes.
- oder -
charCount
ist niedriger als die sich ergebende Anzahl von Zeichen.
Es ist ein Fallback aufgetreten (weitere Informationen finden Sie unter Zeichencodierung in .NET). - und - Für DecoderFallback ist DecoderExceptionFallback festgelegt.
Hinweise
Rufen Sie die GetCharCount Methode auf, um die genaue Arraygröße zu berechnen, die zum GetChars Speichern der resultierenden Zeichen erforderlich ist. Um die maximale Array Größe zu berechnen, müssen Sie die-Methode aufzurufen GetMaxCharCount . Die GetCharCount Methode weist im Allgemeinen weniger Arbeitsspeicher zu, während die GetMaxCharCount Methode im Allgemeinen schneller ausgeführt wird.
Bei der Fehlererkennung führt eine ungültige Sequenz dazu, dass diese Methode eine ArgumentException. Ohne Fehlererkennung werden ungültige Sequenzen ignoriert, und es wird keine Ausnahme ausgelöst.
Wenn der zu decodierte Bytebereich das Bytereihenfolgezeichen (BOM) enthält und das Bytearray von einer Methode eines nicht BOM-fähigen Typs zurückgegeben wurde, wird das Zeichen U+FFFE in das von dieser Methode zurückgegebene Zeichenarray eingeschlossen. Sie können es entfernen, indem Sie die String.TrimStart Methode aufrufen.
Daten, die konvertiert werden sollen, z. B. Daten, die aus einem Datenstrom gelesen werden, sind möglicherweise nur in sequenziellen Blöcken verfügbar. In diesem Fall oder wenn die Menge der Daten so umfangreich ist, dass er in kleinere Blöcke aufgeteilt werden muss, verwendet die Anwendung die Decoder oder die Encoder gebotenen der GetDecoder Methode oder die GetEncoder Methode bzw.
Siehe auch
- GetCharCount(Byte[], Int32, Int32)
- GetMaxCharCount(Int32)
- GetDecoder()
- GetString(Byte[], Int32, Int32)
Gilt für
GetChars(Byte[], Int32, Int32, Char[], Int32)
Decodiert eine Bytefolge aus dem angegebenen Bytearray in das angegebene Zeichenarray.
public:
override int GetChars(cli::array <System::Byte> ^ bytes, int byteIndex, int byteCount, cli::array <char> ^ chars, int charIndex);
public override int GetChars (byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex);
override this.GetChars : byte[] * int * int * char[] * int -> int
Public Overrides Function GetChars (bytes As Byte(), byteIndex As Integer, byteCount As Integer, chars As Char(), charIndex As Integer) As Integer
Parameter
- bytes
- Byte[]
Das Bytearray, das die zu decodierende Bytefolge enthält.
- byteIndex
- Int32
Der Index des ersten zu decodierenden Bytes.
- byteCount
- Int32
Die Anzahl der zu decodierenden Bytes.
- chars
- Char[]
Das Zeichenarray, das die sich ergebenden Zeichen enthalten soll.
- charIndex
- Int32
Der Index, an dem mit dem Schreiben der sich ergebenden Zeichen begonnen werden soll.
Gibt zurück
Die tatsächliche Anzahl der Zeichen, die in chars
geschrieben werden.
Ausnahmen
byteIndex
, byteCount
oder charIndex
ist kleiner als 0 (null).
- oder -
byteindex
und byteCount
geben keinen gültigen Bereich in bytes
an.
- oder -
charIndex
ist kein gültiger Index in chars
.
Die Fehlererkennung ist aktiviert, und bytes
enthält eine ungültige Folge von Bytes.
- oder -
chars
hat von charIndex
bis zum Ende des Arrays nicht genügend Kapazität, um die sich ergebenden Zeichen aufzunehmen.
Es ist ein Fallback aufgetreten (weitere Informationen finden Sie unter Zeichencodierung in .NET). - und - Für DecoderFallback ist DecoderExceptionFallback festgelegt.
Beispiele
Im folgenden Beispiel wird eine Zeichenfolge in ein Bytearray codiert und dann in ein Zeichen Array decodiert.
using namespace System;
using namespace System::Text;
void PrintCountsAndChars( array<Byte>^bytes, Encoding^ enc );
int main()
{
// Create two instances of UTF32Encoding: one with little-endian byte order and one with big-endian byte order.
UTF32Encoding^ u32LE = gcnew UTF32Encoding( false,true,true );
UTF32Encoding^ u32BE = gcnew 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^ myStr = L"za\u0306\u01FD\u03B2\xD8FF\xDCFF";
// barrBE uses the big-endian byte order.
array<Byte>^barrBE = gcnew array<Byte>(u32BE->GetByteCount( myStr ));
u32BE->GetBytes( myStr, 0, myStr->Length, barrBE, 0 );
// barrLE uses the little-endian byte order.
array<Byte>^barrLE = gcnew array<Byte>(u32LE->GetByteCount( myStr ));
u32LE->GetBytes( myStr, 0, myStr->Length, barrLE, 0 );
// Get the char counts and decode the byte arrays.
Console::Write( "BE array with BE encoding : " );
PrintCountsAndChars( barrBE, u32BE );
Console::Write( "LE array with LE encoding : " );
PrintCountsAndChars( barrLE, u32LE );
// Decode the byte arrays using an encoding with a different byte order.
Console::Write( "BE array with LE encoding : " );
try
{
PrintCountsAndChars( barrBE, u32LE );
}
catch ( System::ArgumentException^ e )
{
Console::WriteLine( e->Message );
}
Console::Write( "LE array with BE encoding : " );
try
{
PrintCountsAndChars( barrLE, u32BE );
}
catch ( System::ArgumentException^ e )
{
Console::WriteLine( e->Message );
}
}
void PrintCountsAndChars( array<Byte>^bytes, Encoding^ enc )
{
// Display the name of the encoding used.
Console::Write( "{0,-25} :", enc );
// Display the exact character count.
int iCC = enc->GetCharCount( bytes );
Console::Write( " {0,-3}", iCC );
// Display the maximum character count.
int iMCC = enc->GetMaxCharCount( bytes->Length );
Console::Write( " {0,-3} :", iMCC );
// Decode the bytes and display the characters.
array<Char>^chars = gcnew array<Char>(iCC);
enc->GetChars( bytes, 0, bytes->Length, chars, 0 );
Console::WriteLine( chars );
}
/*
This code produces the following output. The question marks take the place of characters that cannot be displayed at the console.
BE array with BE encoding : System.Text.UTF32Encoding : 7 14 :za??�?
LE array with LE encoding : System.Text.UTF32Encoding : 7 14 :za??�?
BE array with LE encoding : System.Text.UTF32Encoding :Invalid byte was found at byte index 3.
LE array with BE encoding : System.Text.UTF32Encoding :Invalid byte was found at byte index 3.
*/
using System;
using System.Text;
public class SamplesUTF32Encoding {
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)
// a high-surrogate value (U+D8FF)
// a low-surrogate value (U+DCFF)
String myStr = "za\u0306\u01FD\u03B2\uD8FF\uDCFF";
// barrBE uses the big-endian byte order.
byte[] barrBE = new byte[u32BE.GetByteCount( myStr )];
u32BE.GetBytes( myStr, 0, myStr.Length, barrBE, 0 );
// barrLE uses the little-endian byte order.
byte[] barrLE = new byte[u32LE.GetByteCount( myStr )];
u32LE.GetBytes( myStr, 0, myStr.Length, barrLE, 0 );
// Get the char counts and decode the byte arrays.
Console.Write( "BE array with BE encoding : " );
PrintCountsAndChars( barrBE, u32BE );
Console.Write( "LE array with LE encoding : " );
PrintCountsAndChars( barrLE, u32LE );
// Decode the byte arrays using an encoding with a different byte order.
Console.Write( "BE array with LE encoding : " );
try {
PrintCountsAndChars( barrBE, u32LE );
}
catch ( System.ArgumentException e ) {
Console.WriteLine( e.Message );
}
Console.Write( "LE array with BE encoding : " );
try {
PrintCountsAndChars( barrLE, u32BE );
}
catch ( System.ArgumentException e ) {
Console.WriteLine( e.Message );
}
}
public static void PrintCountsAndChars( byte[] bytes, Encoding enc ) {
// Display the name of the encoding used.
Console.Write( "{0,-25} :", enc.ToString() );
// Display the exact character count.
int iCC = enc.GetCharCount( bytes );
Console.Write( " {0,-3}", iCC );
// Display the maximum character count.
int iMCC = enc.GetMaxCharCount( bytes.Length );
Console.Write( " {0,-3} :", iMCC );
// Decode the bytes and display the characters.
char[] chars = new char[iCC];
enc.GetChars( bytes, 0, bytes.Length, chars, 0 );
Console.WriteLine( chars );
}
}
Imports System.Text
Public Class SamplesUTF32Encoding
Public Shared 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)
' a high-surrogate value (U+D8FF)
' a low-surrogate value (U+DCFF)
Dim myStr As String = "za" & ChrW(&H0306) & ChrW(&H01FD) & ChrW(&H03B2) & ChrW(&HD8FF) & ChrW(&HDCFF)
' barrBE uses the big-endian byte order.
' NOTE: In Visual Basic, arrays contain one extra element by default.
' The following line creates an array with the exact number of elements required.
Dim barrBE(u32BE.GetByteCount(myStr) - 1) As Byte
u32BE.GetBytes(myStr, 0, myStr.Length, barrBE, 0)
' barrLE uses the little-endian byte order.
' NOTE: In Visual Basic, arrays contain one extra element by default.
' The following line creates an array with the exact number of elements required.
Dim barrLE(u32LE.GetByteCount(myStr) - 1) As Byte
u32LE.GetBytes(myStr, 0, myStr.Length, barrLE, 0)
' Get the char counts and decode the byte arrays.
Console.Write("BE array with BE encoding : ")
PrintCountsAndChars(barrBE, u32BE)
Console.Write("LE array with LE encoding : ")
PrintCountsAndChars(barrLE, u32LE)
' Decode the byte arrays using an encoding with a different byte order.
Console.Write("BE array with LE encoding : ")
Try
PrintCountsAndChars(barrBE, u32LE)
Catch e As System.ArgumentException
Console.WriteLine(e.Message)
End Try
Console.Write("LE array with BE encoding : ")
Try
PrintCountsAndChars(barrLE, u32BE)
Catch e As System.ArgumentException
Console.WriteLine(e.Message)
End Try
End Sub
Public Shared Sub PrintCountsAndChars(bytes() As Byte, enc As Encoding)
' Display the name of the encoding used.
Console.Write("{0,-25} :", enc.ToString())
' Display the exact character count.
Dim iCC As Integer = enc.GetCharCount(bytes)
Console.Write(" {0,-3}", iCC)
' Display the maximum character count.
Dim iMCC As Integer = enc.GetMaxCharCount(bytes.Length)
Console.Write(" {0,-3} :", iMCC)
' Decode the bytes and display the characters.
Dim chars(iCC) As Char
enc.GetChars(bytes, 0, bytes.Length, chars, 0)
Console.WriteLine(chars)
End Sub
End Class
Hinweise
Um die genaue Arraygröße zu berechnen, die erforderlich GetChars ist, um die resultierenden Zeichen zu speichern, rufen Sie die GetCharCount Methode auf. Um die maximale Array Größe zu berechnen, müssen Sie die-Methode aufzurufen GetMaxCharCount . Die Methode weist im Allgemeinen weniger Arbeitsspeicher zu, während die GetCharCount GetMaxCharCount Methode im Allgemeinen schneller ausgeführt wird.
Bei der Fehlererkennung führt eine ungültige Sequenz dazu, dass diese Methode eine ArgumentException. Ohne Fehlererkennung werden ungültige Sequenzen ignoriert, und keine Ausnahme wird ausgelöst.
Wenn der zu decodierte Bytebereich die Bytereihenfolge (BOM) und das Bytearray durch eine Methode eines nicht-BOM-bewussten Typs zurückgegeben wurde, wird das Zeichen U+FFFE in das von dieser Methode zurückgegebene Zeichenarray eingeschlossen. Sie können es entfernen, indem Sie die String.TrimStart Methode aufrufen.
Daten, die konvertiert werden sollen, z. B. Datenlesedaten aus einem Datenstrom, sind möglicherweise nur in sequenziellen Blöcken verfügbar. In diesem Fall oder wenn die Menge der Daten so umfangreich ist, dass er in kleinere Blöcke aufgeteilt werden muss, verwendet die Anwendung die Decoder oder die Encoder gebotenen der GetDecoder Methode oder die GetEncoder Methode bzw.
Siehe auch
- GetCharCount(Byte[], Int32, Int32)
- GetMaxCharCount(Int32)
- GetDecoder()
- GetString(Byte[], Int32, Int32)