UTF8Encoding.GetByteCount 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
計算編碼一組字元所產生的位元組數量。
多載
| 名稱 | Description |
|---|---|
| GetByteCount(String) |
計算透過編碼指定 String字元所產生的位元組數。 |
| GetByteCount(Char*, Int32) |
計算從指定字元指標開始編碼一組字元所產生的位元組數。 |
| GetByteCount(Char[], Int32, Int32) |
計算從指定字元陣列編碼一組字元所產生的位元組數。 |
GetByteCount(String)
計算透過編碼指定 String字元所產生的位元組數。
public:
override int GetByteCount(System::String ^ chars);
public override int GetByteCount(string chars);
override this.GetByteCount : string -> int
Public Overrides Function GetByteCount (chars As String) As Integer
參數
傳回
編碼指定字元所產生的位元組數。
例外狀況
chars 是 null。
所得的位元組數大於可回傳為整數的最大數值。
錯誤偵測已啟用,但 chars 包含無效的字元序列。
後來出現了備用機制(更多資訊請參見 .NET 中的字元編碼)
-及-
範例
以下範例呼叫 和 GetMaxByteCountGetByteCount(String) 方法來計算編碼字串所需的最大及實際位元組數。 它也會顯示儲存帶有位元組順序標記的位元組串流所需的實際位元組數。
using System;
using System.Text;
class UTF8EncodingExample {
public static void Main() {
String chars = "UTF8 Encoding Example";
Encoding utf8 = Encoding.UTF8;
Console.WriteLine("Bytes needed to encode '{0}':", chars);
Console.WriteLine(" Maximum: {0}",
utf8.GetMaxByteCount(chars.Length));
Console.WriteLine(" Actual: {0}",
utf8.GetByteCount(chars));
Console.WriteLine(" Actual with BOM: {0}",
utf8.GetByteCount(chars) + utf8.GetPreamble().Length);
}
}
// The example displays the following output:
// Bytes needed to encode 'UTF8 Encoding Example':
// Maximum: 66
// Actual: 21
// Actual with BOM: 24
Imports System.Text
Module Example
Public Sub Main()
Dim chars As String = "UTF8 Encoding Example"
Dim utf8 As Encoding = Encoding.UTF8
Console.WriteLine("Bytes needed to encode '{0}':", chars)
Console.WriteLine(" Maximum: {0}",
utf8.GetMaxByteCount(chars.Length))
Console.WriteLine(" Actual: {0}",
utf8.GetByteCount(chars))
Console.WriteLine(" Actual with BOM: {0}",
utf8.GetByteCount(chars) + utf8.GetPreamble().Length)
End Sub
End Module
' The example displays the following output:
' Bytes needed to encode 'UTF8 Encoding Example':
' Maximum: 66
' Actual: 21
' Actual with BOM: 24
備註
要計算儲存所得位元組所需的 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)]
[System.Runtime.InteropServices.ComVisible(false)]
public override int GetByteCount(char* chars, int count);
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
[System.Runtime.InteropServices.ComVisible(false)]
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)>]
[<System.Runtime.InteropServices.ComVisible(false)>]
override this.GetByteCount : nativeptr<char> * int -> int
[<System.CLSCompliant(false)>]
[<System.Security.SecurityCritical>]
[<System.Runtime.InteropServices.ComVisible(false)>]
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 的值比較,該值表示編碼拉丁小寫字元所需的最大位元組數。
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 utf8 = new UTF8Encoding(true);
// 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}",
utf8.GetMaxByteCount(lowercaseEnd - lowercaseStart + 1));
Console.WriteLine(" Actual: {0,5:N0}",
utf8.GetByteCount(chars, uppercaseEnd - uppercaseStart + 1,
lowercaseEnd - lowercaseStart + 1));
Console.WriteLine(" Actual with BOM: {0,5:N0}",
utf8.GetByteCount(chars, uppercaseEnd - uppercaseStart + 1,
lowercaseEnd - lowercaseStart + 1) +
utf8.GetPreamble().Length);
}
}
// The example displays the following output:
// Bytes needed for lowercase Latin characters:
// Maximum: 81
// Actual: 26
// Actual with BOM: 29
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 utf8 As New UTF8Encoding(True)
' 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}",
utf8.GetMaxByteCount(lowercaseEnd - lowercaseStart + 1))
Console.WriteLine(" Actual: {0,5:N0}",
utf8.GetByteCount(chars, uppercaseEnd - uppercaseStart + 1,
lowercaseEnd - lowercaseStart + 1))
Console.WriteLine(" Actual with BOM: {0,5:N0}",
utf8.GetByteCount(chars, uppercaseEnd - uppercaseStart + 1,
lowercaseEnd - lowercaseStart + 1) +
utf8.GetPreamble().Length)
End Sub
End Module
' The example displays the following output:
' Bytes needed for lowercase Latin characters:
' Maximum: 81
' Actual: 26
' Actual with BOM: 29
備註
要計算儲存所得位元組所需的 GetBytes 精確陣列大小,你會呼叫 uses GetByteCount 方法。 要計算最大陣列大小,你呼叫 方法 GetMaxByteCount 。 此 GetByteCount 方法通常分配較少記憶體,而 GetMaxByteCount 執行速度較快。
在錯誤偵測中,若序列無效,該方法會拋出 ArgumentException 異常。 若無錯誤偵測,無效序列會被忽略,且不會拋出例外。
為了確保編碼的位元組在儲存為檔案或串流時能正確解碼,你可以在編碼位元組串流前加上前言。 在位元組串流開頭插入前導碼(例如一連串要寫入檔案的位元組開頭)是開發者的責任,前導碼中的位元組數不會反映在方法回傳 GetByteCount 的值中。