UnicodeEncoding.GetByteCount 方法

定義

計算將一組字元編碼所產生的位元組數目。

多載

GetByteCount(String)

計算將指定字串中的字元編碼所產生的位元組數目。

GetByteCount(Char*, Int32)

計算將起始於指定字元指標的一組字元編碼所產生的位元組數目。

GetByteCount(Char[], Int32, Int32)

計算將指定字元陣列中的一組字元編碼所產生的位元組數目。

GetByteCount(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
String

字串,包含要編碼的一組字元。

傳回

Int32

編碼指定字元所產生的位元組數。

例外狀況

snull

所產生的位元組數目大於可用整數傳回的數目上限。

已啟用錯誤偵測,而 s 包含無效的字元序列。

發生後援 (如需詳細資訊,請參閱 .NET 中的字元編碼) -和- EncoderFallback 設定為 EncoderExceptionFallback

範例

下列範例會呼叫 GetMaxByteCountGetByteCount(String) 方法來計算編碼字串所需的最大和實際位元組數目。 它也會顯示儲存位元組資料流程與位元組順序標記所需的實際位元組數目。

using System;
using System.Text;

class UTF8EncodingExample {
    public static void Main() {
        String chars = "UTF-16 Encoding Example";
        Encoding unicode = Encoding.Unicode;

        Console.WriteLine("Bytes needed to encode '{0}':", chars);
        Console.WriteLine("   Maximum:         {0}",
                          unicode.GetMaxByteCount(chars.Length));
        Console.WriteLine("   Actual:          {0}",
                          unicode.GetByteCount(chars));
        Console.WriteLine("   Actual with BOM: {0}",
                          unicode.GetByteCount(chars) + unicode.GetPreamble().Length);
    }
}
// The example displays the following output:
//       Bytes needed to encode 'UTF-16 Encoding Example':
//          Maximum:         48
//          Actual:          46
//          Actual with BOM: 48
Imports System.Text

Module Example
    Public Sub Main()
        Dim chars As String = "UTF-16 Encoding Example"
        Dim unicode As Encoding = Encoding.Unicode

        Console.WriteLine("Bytes needed to encode '{0}':", chars)
        Console.WriteLine("   Maximum:         {0}",
                          unicode.GetMaxByteCount(chars.Length))
        Console.WriteLine("   Actual:          {0}",
                          unicode.GetByteCount(chars))
        Console.WriteLine("   Actual with BOM: {0}",
                          unicode.GetByteCount(chars) + unicode.GetPreamble().Length)
    End Sub
End Module
' The example displays the following output:
'       Bytes needed to encode 'UTF-16 Encoding Example':
'          Maximum:         48
'          Actual:          46
'          Actual with BOM: 48

備註

若要計算儲存所產生位元組所需的 GetBytes 確切陣列大小,請呼叫 GetByteCount 方法。 若要計算陣列大小上限,請呼叫 GetMaxByteCount 方法。 方法 GetByteCount 通常會配置較少的記憶體,而 GetMaxByteCount 方法通常執行速度較快。

使用錯誤偵測時,不正確序列會導致這個方法擲回 ArgumentException 。 如果沒有錯誤偵測,則會忽略不正確序列,而且不會擲回任何例外狀況。

重要

若要確保編碼的位元組在儲存為檔案或資料流程時正確解碼,您可以將編碼位元組的資料流程加上前置詞。 在位元組資料流程開頭插入前置詞 (,例如要寫入檔案的一系列位元組開頭,) 是開發人員的責任,而且前置詞中的位元組數目不會反映在 方法所 GetByteCount(String) 傳回的值中。

另請參閱

適用於

GetByteCount(Char*, Int32)

重要

此 API 不符合 CLS 規範。

計算將起始於指定字元指標的一組字元編碼所產生的位元組數目。

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]
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)>]
override this.GetByteCount : nativeptr<char> * int -> int
[<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

參數

chars
Char*

要編碼的第一個字元指標。

count
Int32

要編碼的字元數。

傳回

Int32

編碼指定字元所產生的位元組數。

屬性

例外狀況

charsnull

count 小於零。

-或- 所產生的位元組數目大於可用整數傳回的數目上限。

已啟用錯誤偵測,而 chars 包含無效的字元序列。

發生後援 (如需詳細資訊,請參閱 .NET 中的字元編碼) -和- EncoderFallback 設定為 EncoderExceptionFallback

備註

若要計算儲存所產生位元組所需的確切陣列大小 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

要編碼的字元數。

傳回

Int32

編碼指定字元所產生的位元組數。

例外狀況

charsnull (Nothing)。

indexcount 小於零。

-或- indexcount 不代表 chars 中有效的範圍。

-或- 所產生的位元組數目大於可用整數傳回的數目上限。

已啟用錯誤偵測,而 chars 包含無效的字元序列。

發生後援 (如需詳細資訊,請參閱 .NET 中的字元編碼) -和- EncoderFallback 設定為 EncoderExceptionFallback

範例

下列範例會以拉丁大寫和小寫字元填入陣列,並呼叫 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 unicode = Encoding.Unicode;

      // 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}",
                        unicode.GetMaxByteCount(lowercaseEnd - lowercaseStart + 1));
      Console.WriteLine("   Actual:          {0,5:N0}",
                        unicode.GetByteCount(chars, uppercaseEnd - uppercaseStart + 1,
                                          lowercaseEnd - lowercaseStart + 1));
      Console.WriteLine("   Actual with BOM: {0,5:N0}",
                        unicode.GetByteCount(chars, uppercaseEnd - uppercaseStart + 1,
                                          lowercaseEnd - lowercaseStart + 1) +
                                          unicode.GetPreamble().Length);
   }
}
// The example displays the following output:
//       Bytes needed for lowercase Latin characters:
//          Maximum:            54
//          Actual:             52
//          Actual with BOM:    54
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 unicode As Encoding = Encoding.Unicode
      
      ' 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}",
                          unicode.GetMaxByteCount(lowercaseEnd - lowercaseStart + 1))
        Console.WriteLine("   Actual:          {0,5:N0}",
                          unicode.GetByteCount(chars, uppercaseEnd - uppercaseStart + 1,
                                            lowercaseEnd - lowercaseStart + 1))
        Console.WriteLine("   Actual with BOM: {0,5:N0}",
                          unicode.GetByteCount(chars, uppercaseEnd - uppercaseStart + 1,
                                            lowercaseEnd - lowercaseStart + 1) +
                                            unicode.GetPreamble().Length)
   End Sub
End Module
' The example displays the following output:
'       Bytes needed for lowercase Latin characters:
'          Maximum:            54
'          Actual:             52
'          Actual with BOM:    54

備註

若要計算儲存所產生位元組所需的 GetBytes 確切陣列大小,應用程式會使用 GetByteCount 。 若要計算陣列大小上限,請呼叫 GetMaxByteCount 方法。 方法 GetByteCount 通常會配置較少的記憶體,而 GetMaxByteCount 方法通常執行速度較快。

啟用錯誤偵測後,不正確序列會導致此方法擲回 ArgumentException 。 如果沒有錯誤偵測,則會忽略不正確序列,而且不會擲回任何例外狀況。

若要確保編碼的位元組在儲存為檔案或資料流程時正確解碼,您可以將編碼位元組的資料流程加上前置詞。 在位元組資料流程開頭插入前置詞 (,例如要寫入檔案的一系列位元組開頭,) 是開發人員的責任,而且前置詞中的位元組數目不會反映在 方法所 GetByteCount(Char[], Int32, Int32) 傳回的值中。

另請參閱

適用於