UTF8Encoding.GetByteCount UTF8Encoding.GetByteCount UTF8Encoding.GetByteCount UTF8Encoding.GetByteCount Method

定義

計算將一組字元編碼所產生的位元組數目。Calculates the number of bytes produced by encoding a set of characters.

多載

GetByteCount(ReadOnlySpan<Char>) GetByteCount(ReadOnlySpan<Char>) GetByteCount(ReadOnlySpan<Char>) GetByteCount(ReadOnlySpan<Char>)

計算將指定字元範圍編碼所產生的位元組數目。Calculates the number of bytes produced by encoding the specified character span.

GetByteCount(String) GetByteCount(String) GetByteCount(String) GetByteCount(String)

計算將指定 String 中的字元編碼所產生的位元組數目。Calculates the number of bytes produced by encoding the characters in the specified String.

GetByteCount(Char*, Int32) GetByteCount(Char*, Int32) GetByteCount(Char*, Int32)

計算將起始於指定字元指標的一組字元編碼所產生的位元組數目。Calculates the number of bytes produced by encoding a set of characters starting at the specified character pointer.

GetByteCount(Char[], Int32, Int32) GetByteCount(Char[], Int32, Int32) GetByteCount(Char[], Int32, Int32) GetByteCount(Char[], Int32, Int32)

計算將指定字元陣列中的一組字元編碼所產生的位元組數目。Calculates the number of bytes produced by encoding a set of characters from the specified character array.

GetByteCount(ReadOnlySpan<Char>) GetByteCount(ReadOnlySpan<Char>) GetByteCount(ReadOnlySpan<Char>) GetByteCount(ReadOnlySpan<Char>)

計算將指定字元範圍編碼所產生的位元組數目。Calculates the number of bytes produced by encoding the specified character span.

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>

範圍,包含要編碼的一組字元。The span that contains the set of characters to encode.

傳回

編碼指定字元範圍所產生的位元組數目。The number of bytes produced by encoding the specified character span.

例外狀況

已啟用錯誤偵測,而 chars 包含無效的字元序列。Error detection is enabled, and chars contains an invalid sequence of characters.

備註

若要計算所需GetBytes的確切大小以儲存產生的位元組, 您可以GetByteCount呼叫方法。To calculate the exact size required by GetBytes to store the resulting bytes, you call the GetByteCount method. 若要計算大小上限, 您可以呼叫GetMaxByteCount方法。To calculate the maximum size, you call the GetMaxByteCount method. 方法通常會配置較少的GetMaxByteCount記憶體, 而方法通常會更快速地執行。 GetByteCountThe GetByteCount method generally allocates less memory, while the GetMaxByteCount method generally executes faster.

使用錯誤偵測時, 不正確順序會導致這個方法擲ArgumentException回例外狀況。With error detection, an invalid sequence causes this method to throw an ArgumentException exception. 若未偵測到錯誤, 則會忽略不正確序列, 而且不會擲回任何例外狀況。Without error detection, invalid sequences are ignored, and no exception is thrown.

為確保編碼的位元組會在儲存為檔案或資料流程時正確解碼, 您可以在編碼的位元組資料流程前面加上前置字元。To ensure that the encoded bytes are decoded properly when they are saved as a file or as a stream, you can prefix a stream of encoded bytes with a preamble. 在位元組資料流程開頭插入前導碼 (例如, 在要寫入檔案的一系列位元組開頭) 是開發人員的責任, 而前序的位元組數目不會反映在GetByteCount方法所傳回的值中。Inserting the preamble at the beginning of a byte stream (such as at the beginning of a series of bytes to be written to a file) is the developer's responsibility, and the number of bytes in the preamble is not reflected in the value returned by the GetByteCount method.

GetByteCount(String) GetByteCount(String) GetByteCount(String) GetByteCount(String)

計算將指定 String 中的字元編碼所產生的位元組數目。Calculates the number of bytes produced by encoding the characters in the specified 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 String String

String,包含要編碼的一組字元。The String containing the set of characters to encode.

傳回

編碼指定字元所產生的位元組數。The number of bytes produced by encoding the specified characters.

例外狀況

所產生的位元組數目大於可用整數傳回的數目上限。The resulting number of bytes is greater than the maximum number that can be returned as an integer.

已啟用錯誤偵測,而 chars 包含無效的字元序列。Error detection is enabled, and chars contains an invalid sequence of characters.

範例

下列範例會呼叫GetMaxByteCountGetByteCount(String)方法, 以計算將字串編碼所需的最大和實際位元組數目。The following example calls the GetMaxByteCount and GetByteCount(String) methods to calculate the maximum and actual number of bytes required to encode a string. 它也會顯示以位元組順序標記儲存位元組資料流程所需的實際位元組數目。It also displays the actual number of bytes required to store a byte stream with a byte order mark.

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呼叫方法。To calculate the exact array size required by GetBytes to store the resulting bytes, you call the GetByteCount method. 若要計算陣列大小上限, 您可以呼叫GetMaxByteCount方法。To calculate the maximum array size, you call the GetMaxByteCount method. 方法通常會配置較少的GetMaxByteCount記憶體, 而方法通常會更快速地執行。 GetByteCountThe GetByteCount method generally allocates less memory, while the GetMaxByteCount method generally executes faster.

使用錯誤偵測時, 不正確順序會導致這個方法擲ArgumentException回例外狀況。With error detection, an invalid sequence causes this method to throw an ArgumentException exception. 若未偵測到錯誤, 則會忽略不正確序列, 而且不會擲回任何例外狀況。Without error detection, invalid sequences are ignored, and no exception is thrown.

為確保編碼的位元組會在儲存為檔案或資料流程時正確解碼, 您可以在編碼的位元組資料流程前面加上前置字元。To ensure that the encoded bytes are decoded properly when they are saved as a file or as a stream, you can prefix a stream of encoded bytes with a preamble. 在位元組資料流程開頭插入前導碼 (例如, 在要寫入檔案的一系列位元組開頭) 是開發人員的責任, 而前序的位元組數目不會反映在GetByteCount方法所傳回的值中。Inserting the preamble at the beginning of a byte stream (such as at the beginning of a series of bytes to be written to a file) is the developer's responsibility, and the number of bytes in the preamble is not reflected in the value returned by the GetByteCount method.

另請參閱

GetByteCount(Char*, Int32) GetByteCount(Char*, Int32) GetByteCount(Char*, Int32)

重要

此 API 不符合 CLS 規範。

計算將起始於指定字元指標的一組字元編碼所產生的位元組數目。Calculates the number of bytes produced by encoding a set of characters starting at the specified character pointer.

public:
 override int GetByteCount(char* chars, int count);
[System.CLSCompliant(false)]
[System.Runtime.InteropServices.ComVisible(false)]
[System.Security.SecurityCritical]
public override int GetByteCount (char* chars, int count);
override this.GetByteCount : nativeptr<char> * int -> int

參數

chars
Char*

要編碼的第一個字元指標。A pointer to the first character to encode.

count
Int32 Int32 Int32 Int32

要編碼的字元數。The number of characters to encode.

傳回

編碼指定字元所產生的位元組數。The number of bytes produced by encoding the specified characters.

例外狀況

count 小於零。count is less than zero.

-或--or- 所產生的位元組數目大於可用整數傳回的數目上限。The resulting number of bytes is greater than the maximum number that can be returned as an integer.

已啟用錯誤偵測,而 chars 包含無效的字元序列。Error detection is enabled, and chars contains an invalid sequence of characters.

備註

若要計算GetBytes方法用來儲存產生的位元組所需的確切陣列大小, 您可以GetByteCount呼叫方法。To calculate the exact array size required by the GetBytes method to store the resulting bytes, you call the GetByteCount method. 若要計算陣列大小上限, 您可以呼叫GetMaxByteCount方法。To calculate the maximum array size, you call the GetMaxByteCount method. 方法通常會配置較少的GetMaxByteCount記憶體, 而方法通常會更快速地執行。 GetByteCountThe GetByteCount method generally allocates less memory, while the GetMaxByteCount method generally executes faster.

使用錯誤偵測時, 不正確順序會導致這個方法擲ArgumentException回例外狀況。With error detection, an invalid sequence causes this method to throw an ArgumentException exception. 若未偵測到錯誤, 則會忽略不正確序列, 而且不會擲回任何例外狀況。Without error detection, invalid sequences are ignored, and no exception is thrown.

為確保編碼的位元組會在儲存為檔案或資料流程時正確解碼, 您可以在編碼的位元組資料流程前面加上前置字元。To ensure that the encoded bytes are decoded properly when they are saved as a file or as a stream, you can prefix a stream of encoded bytes with a preamble. 在位元組資料流程開頭插入前導碼 (例如, 在要寫入檔案的一系列位元組開頭) 是開發人員的責任, 而前序的位元組數目不會反映在GetByteCount方法所傳回的值中。Inserting the preamble at the beginning of a byte stream (such as at the beginning of a series of bytes to be written to a file) is the developer's responsibility, and the number of bytes in the preamble is not reflected in the value returned by the GetByteCount method.

安全性

SecurityCriticalAttribute
需要完全信任立即呼叫者。Requires full trust for the immediate caller. 部分信任或透明的程式碼無法使用這個成員。This member cannot be used by partially trusted or transparent code.

另請參閱

GetByteCount(Char[], Int32, Int32) GetByteCount(Char[], Int32, Int32) GetByteCount(Char[], Int32, Int32) GetByteCount(Char[], Int32, Int32)

計算將指定字元陣列中的一組字元編碼所產生的位元組數目。Calculates the number of bytes produced by encoding a set of characters from the specified character array.

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[]

包含要解碼之一組字元的字元陣列。The character array containing the set of characters to encode.

index
Int32 Int32 Int32 Int32

要編碼的第一個字元索引。The index of the first character to encode.

count
Int32 Int32 Int32 Int32

要編碼的字元數。The number of characters to encode.

傳回

編碼指定字元所產生的位元組數。The number of bytes produced by encoding the specified characters.

例外狀況

indexcount 小於零。index or count is less than zero.

-或--or- indexcount 不代表 chars 中有效的範圍。index and count do not denote a valid range in chars.

-或--or- 所產生的位元組數目大於可用整數傳回的數目上限。The resulting number of bytes is greater than the maximum number that can be returned as an integer.

已啟用錯誤偵測,而 chars 包含無效的字元序列。Error detection is enabled, and chars contains an invalid sequence of characters.

範例

下列範例會以拉丁大寫和小寫字元填入陣列, 並呼叫GetByteCount(Char[], Int32, Int32)方法來判斷編碼拉丁小寫字元所需的位元組數目。The following example populates an array with a Latin uppercase and lowercase characters and calls the GetByteCount(Char[], Int32, Int32) method to determine the number of bytes needed to encode the Latin lowercase characters. 然後, 它會顯示這項資訊, 以及加入位元組順序標記時所需的總位元組數。It then displays this information along with the total number of bytes needed if a byte order mark is added. 它會將這個數位與GetMaxByteCount方法所傳回的值進行比較, 這表示編碼拉丁小寫字元所需的最大位元組數目。It compares this number with the value returned by the GetMaxByteCount method, which indicates maximum number of bytes needed to encode the Latin lowercase characters.

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的確切陣列大小以儲存產生的位元組, 您可以呼叫使用GetByteCount方法。To calculate the exact array size required by GetBytes to store the resulting bytes, you call the uses GetByteCount method. 若要計算陣列大小上限, 您可以呼叫GetMaxByteCount方法。To calculate the maximum array size, you call the GetMaxByteCount method. 方法通常會配置較少的GetMaxByteCount記憶體, 而方法通常會更快速地執行。 GetByteCountThe GetByteCount method generally allocates less memory, while the GetMaxByteCount method generally executes faster.

使用錯誤偵測時, 不正確順序會導致這個方法擲ArgumentException回例外狀況。With error detection, an invalid sequence causes this method to throw an ArgumentException exception. 若未偵測到錯誤, 則會忽略不正確序列, 而且不會擲回任何例外狀況。Without error detection, invalid sequences are ignored, and no exception is thrown.

為確保編碼的位元組會在儲存為檔案或資料流程時正確解碼, 您可以在編碼的位元組資料流程前面加上前置字元。To ensure that the encoded bytes are decoded properly when they are saved as a file or as a stream, you can prefix a stream of encoded bytes with a preamble. 在位元組資料流程開頭插入前導碼 (例如, 在要寫入檔案的一系列位元組開頭) 是開發人員的責任, 而前序的位元組數目不會反映在GetByteCount方法所傳回的值中。Inserting the preamble at the beginning of a byte stream (such as at the beginning of a series of bytes to be written to a file) is the developer's responsibility, and the number of bytes in the preamble is not reflected in the value returned by the GetByteCount method.

另請參閱

適用於