UTF32Encoding.GetByteCount 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
計算編碼一組字元所產生的位元組數量。
多載
| 名稱 | Description |
|---|---|
| GetByteCount(String) |
計算透過編碼指定 String字元所產生的位元組數。 |
| GetByteCount(Char*, Int32) |
計算從指定字元指標開始編碼一組字元所產生的位元組數。 |
| GetByteCount(Char[], Int32, Int32) |
計算從指定字元陣列編碼一組字元所產生的位元組數。 |
GetByteCount(String)
計算透過編碼指定 String字元所產生的位元組數。
public:
override int GetByteCount(System::String ^ s);
public override int GetByteCount(string s);
override this.GetByteCount : string -> int
Public Overrides Function GetByteCount (s As String) As Integer
參數
傳回
編碼指定字元所產生的位元組數。
例外狀況
s 是 null。
所得的位元組數大於可回傳為整數的最大數值。
錯誤偵測已啟用,但 s 包含無效的字元序列。
後來出現了備用機制(更多資訊請參見 .NET 中的字元編碼)
-及-
範例
以下範例呼叫 和 GetMaxByteCountGetByteCount(String) 方法來計算編碼字串所需的最大及實際位元組數。 它也會顯示儲存帶有位元組順序標記的位元組串流所需的實際位元組數。
using System;
using System.Text;
class UTF8EncodingExample {
public static void Main() {
String chars = "UTF-32 Encoding Example";
Encoding enc = Encoding.UTF32;
Console.WriteLine("Bytes needed to encode '{0}':", chars);
Console.WriteLine(" Maximum: {0}",
enc.GetMaxByteCount(chars.Length));
Console.WriteLine(" Actual: {0}",
enc.GetByteCount(chars));
Console.WriteLine(" Actual with BOM: {0}",
enc.GetByteCount(chars) + enc.GetPreamble().Length);
}
}
// The example displays the following output:
// Bytes needed to encode 'UTF-32 Encoding Example':
// Maximum: 96
// Actual: 92
// Actual with BOM: 96
Imports System.Text
Module Example
Public Sub Main()
Dim chars As String = "UTF-32 Encoding Example"
Dim enc As Encoding = Encoding.UTF32
Console.WriteLine("Bytes needed to encode '{0}':", chars)
Console.WriteLine(" Maximum: {0}",
enc.GetMaxByteCount(chars.Length))
Console.WriteLine(" Actual: {0}",
enc.GetByteCount(chars))
Console.WriteLine(" Actual with BOM: {0}",
enc.GetByteCount(chars) + enc.GetPreamble().Length)
End Sub
End Module
' The example displays the following output:
' Bytes needed to encode 'UTF-32 Encoding Example':
' Maximum: 96
' Actual: 92
' Actual with BOM: 96
備註
要計算儲存所得位元組所需的 GetBytes 精確陣列大小,你就呼叫了 方法 GetByteCount 。 要計算最大陣列大小,你呼叫 方法 GetMaxByteCount 。 此 GetByteCount 方法通常分配較少記憶體,而 GetMaxByteCount 執行速度較快。
在錯誤檢測中,若序列無效,該方法會拋出 ArgumentException。 若無錯誤偵測,無效序列會被忽略,且不會拋出例外。
為了確保編碼的位元組在儲存為檔案或串流時能正確解碼,你可以在編碼位元組串流前加上前言。 在位元組串流開頭插入前導碼(例如一連串要寫入檔案的位元組開頭)是開發者的責任,前導碼中的位元組數不會反映在方法回傳 GetByteCount 的值中。
另請參閱
適用於
GetByteCount(Char*, Int32)
重要
此 API 不符合 CLS 規範。
計算從指定字元指標開始編碼一組字元所產生的位元組數。
public:
override int GetByteCount(char* chars, int count);
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
public override int GetByteCount(char* chars, int count);
[System.CLSCompliant(false)]
public override int GetByteCount(char* chars, int count);
[<System.CLSCompliant(false)>]
[<System.Security.SecurityCritical>]
override this.GetByteCount : nativeptr<char> * int -> int
[<System.CLSCompliant(false)>]
override this.GetByteCount : nativeptr<char> * int -> int
參數
- chars
- Char*
一個指向第一個要編碼字元的指標。
- count
- Int32
需要編碼的字元數。
傳回
編碼指定字元所產生的位元組數。
- 屬性
例外狀況
chars 是 null。
錯誤偵測已啟用,但 chars 包含無效的字元序列。
後來出現了備用機制(更多資訊請參見 .NET 中的字元編碼)
-及-
備註
要計算儲存所得位元組所需的 GetBytes 精確陣列大小,你就呼叫了 方法 GetByteCount 。 要計算最大陣列大小,你將 。GetMaxByteCount 此 GetByteCount 方法通常分配較少記憶體,而 GetMaxByteCount 執行速度較快。
在錯誤檢測中,若序列無效,該方法會拋出 ArgumentException。 若無錯誤偵測,無效序列會被忽略,且不會拋出例外。
為了確保編碼的位元組在儲存為檔案或串流時能正確解碼,你可以在編碼位元組串流前加上前言。 在位元組串流開頭插入前導碼(例如一連串要寫入檔案的位元組開頭)是開發者的責任,前導碼中的位元組數不會反映在方法回傳 GetByteCount 的值中。
另請參閱
適用於
GetByteCount(Char[], Int32, Int32)
計算從指定字元陣列編碼一組字元所產生的位元組數。
public:
override int GetByteCount(cli::array <char> ^ chars, int index, int count);
public override int GetByteCount(char[] chars, int index, int count);
override this.GetByteCount : char[] * int * int -> int
Public Overrides Function GetByteCount (chars As Char(), index As Integer, count As Integer) As Integer
參數
- chars
- Char[]
包含要編碼字元集合的字元陣列。
- index
- Int32
第一個要編碼字元的索引。
- count
- Int32
需要編碼的字元數。
傳回
編碼指定字元所產生的位元組數。
例外狀況
chars 是 null。
index 或 count 小於零。
-或-
index 且 count 不表示在 中的 chars有效範圍。
-或-
所得的位元組數大於可回傳為整數的最大數值。
錯誤偵測已啟用,但 chars 包含無效的字元序列。
後來出現了備用機制(更多資訊請參見 .NET 中的字元編碼)
-及-
範例
以下範例將拉丁大寫與小寫字元填充陣列,並呼叫該 GetByteCount(Char[], Int32, Int32) 方法來決定編碼拉丁小寫字元所需的位元組數。 接著會顯示這些資訊,以及如果新增位元組順序標記所需的總位元組數。 它會將此數字與方法回傳 GetMaxByteCount 的值比較,該值表示編碼拉丁小寫字元所需的最大位元組數。 以下範例將希臘字母與西里爾字母組合填充陣列,並呼叫 GetByteCount(Char[], Int32, Int32) 該方法來決定編碼西里爾字母所需的位元組數。 接著會顯示這些資訊,以及如果新增位元組順序標記所需的總位元組數。 它會將此數字與方法回傳 GetMaxByteCount 的值比較,後者表示編碼西里爾字母所需的最大位元組數。
using System;
using System.Text;
public class Example
{
public static void Main()
{
int uppercaseStart = 0x0041;
int uppercaseEnd = 0x005a;
int lowercaseStart = 0x0061;
int lowercaseEnd = 0x007a;
// Instantiate a UTF8 encoding object with BOM support.
Encoding enc = Encoding.UTF32;
// Populate array with characters.
char[] chars = new char[lowercaseEnd - lowercaseStart + uppercaseEnd - uppercaseStart + 2];
int index = 0;
for (int ctr = uppercaseStart; ctr <= uppercaseEnd; ctr++) {
chars[index] = (char)ctr;
index++;
}
for (int ctr = lowercaseStart; ctr <= lowercaseEnd; ctr++) {
chars[index] = (char)ctr;
index++;
}
// Display the bytes needed for the lowercase characters.
Console.WriteLine("Bytes needed for lowercase Latin characters:");
Console.WriteLine(" Maximum: {0,5:N0}",
enc.GetMaxByteCount(lowercaseEnd - lowercaseStart + 1));
Console.WriteLine(" Actual: {0,5:N0}",
enc.GetByteCount(chars, uppercaseEnd - uppercaseStart + 1,
lowercaseEnd - lowercaseStart + 1));
Console.WriteLine(" Actual with BOM: {0,5:N0}",
enc.GetByteCount(chars, uppercaseEnd - uppercaseStart + 1,
lowercaseEnd - lowercaseStart + 1) +
enc.GetPreamble().Length);
}
}
// The example displays the following output:
// Bytes needed for lowercase Latin characters:
// Maximum: 108
// Actual: 104
// Actual with BOM: 108
Imports System.Text
Module Example
Public Sub Main()
Dim uppercaseStart As Integer = &h0041
Dim uppercaseEnd As Integer = &h005a
Dim lowercaseStart As Integer = &h0061
Dim lowercaseEnd As Integer = &h007a
' Instantiate a UTF8 encoding object with BOM support.
Dim enc As Encoding = Encoding.UTF32
' Populate array with characters.
Dim chars(lowercaseEnd - lowercaseStart + uppercaseEnd - uppercaseStart + 1) As Char
Dim index As Integer = 0
For ctr As Integer = uppercaseStart To uppercaseEnd
chars(index) = ChrW(ctr)
index += 1
Next
For ctr As Integer = lowercaseStart To lowercaseEnd
chars(index) = ChrW(ctr)
index += 1
Next
' Display the bytes needed for the lowercase characters.
Console.WriteLine("Bytes needed for lowercase Latin characters:")
Console.WriteLine(" Maximum: {0,5:N0}",
enc.GetMaxByteCount(lowercaseEnd - lowercaseStart + 1))
Console.WriteLine(" Actual: {0,5:N0}",
enc.GetByteCount(chars, uppercaseEnd - uppercaseStart + 1,
lowercaseEnd - lowercaseStart + 1))
Console.WriteLine(" Actual with BOM: {0,5:N0}",
enc.GetByteCount(chars, uppercaseEnd - uppercaseStart + 1,
lowercaseEnd - lowercaseStart + 1) +
enc.GetPreamble().Length)
End Sub
End Module
' The example displays the following output:
' Bytes needed for lowercase Latin characters:
' Maximum: 108
' Actual: 104
' Actual with BOM: 108
備註
要計算儲存所得位元組所需的 GetBytes 精確陣列大小,你就呼叫了 方法 GetByteCount 。 要計算最大陣列大小,你呼叫 方法 GetMaxByteCount 。 此 GetByteCount 方法通常分配較少記憶體,而 GetMaxByteCount 執行速度較快。
在錯誤檢測中,若序列無效,該方法會拋出 ArgumentException。 若無錯誤偵測,無效序列會被忽略,且不會拋出例外。
為了確保編碼的位元組在儲存為檔案或串流時能正確解碼,你可以在編碼位元組串流前加上前言。 在位元組串流開頭插入前導碼(例如一連串要寫入檔案的位元組開頭)是開發者的責任,前導碼中的位元組數不會反映在方法回傳 GetByteCount 的值中。