UTF32Encoding.GetChars 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將一串位元組解碼成一組字元。
多載
| 名稱 | Description |
|---|---|
| GetChars(Byte*, Int32, Char*, Int32) |
將從指定位元組指標開始的一串位元組解碼成一組字元,這些字元從指定字元指標開始儲存。 |
| GetChars(Byte[], Int32, Int32, Char[], Int32) |
將指定位元組陣列的一串位元組解碼到指定的字元陣列。 |
GetChars(Byte*, Int32, Char*, Int32)
重要
此 API 不符合 CLS 規範。
將從指定位元組指標開始的一串位元組解碼成一組字元,這些字元從指定字元指標開始儲存。
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
參數
- bytes
- Byte*
一個指向第一個位元組的指標來解碼。
- byteCount
- Int32
需要解碼的位元組數。
- chars
- Char*
指向開始寫入所得字元集合的位置指標。
- charCount
- Int32
要寫入的字元數上限。
傳回
在 所示 chars位置書寫的實際字元數。
- 屬性
例外狀況
byteCount 或 charCount 小於零。
後來出現了備用機制(更多資訊請參見 .NET 中的字元編碼)
-及-
備註
要計算儲存所得字元所需的 GetChars 精確陣列大小,請呼叫該 GetCharCount 方法。 要計算最大陣列大小,請呼叫該 GetMaxCharCount 方法。 此 GetCharCount 方法通常分配較少記憶體,而 GetMaxCharCount 執行速度較快。
在錯誤檢測中,若序列無效,該方法會拋出 ArgumentException。 若無錯誤偵測,無效序列會被忽略,且不會拋出例外。
若解碼的位元組範圍包含位元組順序標記(BOM),且位元組陣列是由非 BOM 識別型態的方法回傳,則字元 U+FFFE 會包含在該方法回傳的字元陣列中。 你可以透過呼叫 String.TrimStart 該方法來移除它。
待轉換的資料,例如從串流讀取的資料,可能只能以連續區塊形式取得。 在這種情況下,或資料量龐大到需要分割成較小區塊時,應用程式會分別使用Decoder方法或Encoder方法所提供的 GetDecoder 或 GetEncoder 。
另請參閱
- GetCharCount(Byte[], Int32, Int32)
- GetMaxCharCount(Int32)
- GetDecoder()
- GetString(Byte[], Int32, Int32)
適用於
GetChars(Byte[], Int32, Int32, Char[], Int32)
將指定位元組陣列的一串位元組解碼到指定的字元陣列。
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
參數
- bytes
- Byte[]
包含要解碼的位元組序列的位元組陣列。
- byteIndex
- Int32
第一個要解碼的位元組的索引。
- byteCount
- Int32
需要解碼的位元組數。
- chars
- Char[]
字元陣列用來包含所產生的字元集合。
- charIndex
- Int32
索引,從哪裡開始寫入結果字元集合。
傳回
實際寫入 chars的字元數。
例外狀況
byteIndex 或 byteCount 或 charIndex 小於零。
-或-
byteIndex 且 byteCount 不表示在 中的 bytes有效範圍。
-或-
charIndex 在 中 不是有效的指標 chars。
後來出現了備用機制(更多資訊請參見 .NET 中的字元編碼)
-及-
範例
以下範例將字串編碼成一個位元組陣列,然後將位元組解碼成一個字元陣列。
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
備註
要計算儲存所得字元所需的 GetChars 精確陣列大小,請呼叫該 GetCharCount 方法。 要計算最大陣列大小,請呼叫該 GetMaxCharCount 方法。 此 GetCharCount 方法通常分配較少記憶體,而 GetMaxCharCount 執行速度較快。
在錯誤檢測中,若序列無效,該方法會拋出 ArgumentException。 若無錯誤偵測,無效序列會被忽略,且不會拋出例外。
若解碼的位元組範圍包含位元組順序標記(BOM),且位元組陣列是由非 BOM 識別型態的方法回傳,則字元 U+FFFE 會包含在該方法回傳的字元陣列中。 你可以透過呼叫 String.TrimStart 該方法來移除它。
待轉換的資料,例如從串流讀取的資料,可能只能以連續區塊形式取得。 在這種情況下,或資料量龐大到需要分割成較小區塊時,應用程式會分別使用Decoder方法或Encoder方法所提供的 GetDecoder 或 GetEncoder 。
另請參閱
- GetCharCount(Byte[], Int32, Int32)
- GetMaxCharCount(Int32)
- GetDecoder()
- GetString(Byte[], Int32, Int32)