UTF8Encoding.GetByteCount 方法

定義

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

多載

GetByteCount(ReadOnlySpan<Char>)

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

GetByteCount(String)

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

GetByteCount(Char*, Int32)

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

GetByteCount(Char[], Int32, Int32)

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

GetByteCount(ReadOnlySpan<Char>)

來源:
UTF8Encoding.cs
來源:
UTF8Encoding.cs
來源:
UTF8Encoding.cs

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

public:
 override int GetByteCount(ReadOnlySpan<char> chars);
public override int GetByteCount (ReadOnlySpan<char> chars);
override this.GetByteCount : ReadOnlySpan<char> -> int
Public Overrides Function GetByteCount (chars As ReadOnlySpan(Of Char)) As Integer

參數

chars
ReadOnlySpan<Char>

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

傳回

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

例外狀況

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

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

-和-

EncoderFallback 設定為 EncoderExceptionFallback

備註

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

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

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

適用於

GetByteCount(String)

來源:
UTF8Encoding.cs
來源:
UTF8Encoding.cs
來源:
UTF8Encoding.cs

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

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

傳回

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

例外狀況

charsnull

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

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

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

-和-

EncoderFallback 設定為 EncoderExceptionFallback

範例

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

來源:
UTF8Encoding.cs
來源:
UTF8Encoding.cs
來源:
UTF8Encoding.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.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)>]
[<System.Security.SecurityCritical>]
override this.GetByteCount : nativeptr<char> * int -> int
[<System.CLSCompliant(false)>]
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

要編碼的字元數。

傳回

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

屬性

例外狀況

charsnull

count 小於零。

-或-

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

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

發生 Fallback (如需完整說明,請參閱 .NET 中的字元編碼)。

-和-

EncoderFallback 設定為 EncoderExceptionFallback

備註

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

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

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

另請參閱

適用於

GetByteCount(Char[], Int32, Int32)

來源:
UTF8Encoding.cs
來源:
UTF8Encoding.cs
來源:
UTF8Encoding.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 傳回的值,這表示編碼拉丁小寫字元所需的位元組數目上限。

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

另請參閱

適用於