UTF32Encoding.GetByteCount 方法

定義

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

多載

GetByteCount(String)

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

GetByteCount(Char*, Int32)

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

GetByteCount(Char[], Int32, Int32)

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

GetByteCount(String)

來源:
UTF32Encoding.cs
來源:
UTF32Encoding.cs
來源:
UTF32Encoding.cs

計算將指定 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

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

傳回

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

例外狀況

snull

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

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

發生後援 (如需詳細資訊,請參閱 .NET 中的字元編碼)

-和-

EncoderFallback 設定為 EncoderExceptionFallback

範例

下列範例會呼叫 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)

來源:
UTF32Encoding.cs
來源:
UTF32Encoding.cs
來源:
UTF32Encoding.cs

重要

此 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

要編碼的字元數。

傳回

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

屬性

例外狀況

charsnull

count 小於零。

-或-

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

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

發生後援 (如需詳細資訊,請參閱 .NET 中的字元編碼)

-和-

EncoderFallback 設定為 EncoderExceptionFallback

備註

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

發生錯誤偵測時,不正確序列會導致這個方法擲回 ArgumentException 。 若未偵測錯誤,則會忽略不正確序列,而且不會擲回例外狀況。

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

另請參閱

適用於

GetByteCount(Char[], Int32, Int32)

來源:
UTF32Encoding.cs
來源:
UTF32Encoding.cs
來源:
UTF32Encoding.cs

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

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

要編碼的字元數。

傳回

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

例外狀況

charsnull

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 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 傳回的值中。

另請參閱

適用於