UTF7Encoding.GetByteCount 方法

定義

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

多載

GetByteCount(String)

編碼指定 String 物件中的字元,從而計算所產生的位元組數。

GetByteCount(Char*, Int32)

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

GetByteCount(Char[], Int32, Int32)

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

GetByteCount(String)

Source:
UTF7Encoding.cs
Source:
UTF7Encoding.cs
Source:
UTF7Encoding.cs

編碼指定 String 物件中的字元,從而計算所產生的位元組數。

public:
 override int GetByteCount(System::String ^ s);
public override int GetByteCount (string s);
[System.Runtime.InteropServices.ComVisible(false)]
public override int GetByteCount (string s);
override this.GetByteCount : string -> int
[<System.Runtime.InteropServices.ComVisible(false)>]
override this.GetByteCount : string -> int
Public Overrides Function GetByteCount (s As String) As Integer

參數

s
String

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

傳回

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

屬性

例外狀況

snull (Nothing)。

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

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

-和-

EncoderFallback 設定為 EncoderExceptionFallback

範例

下列程式碼範例示範如何使用 GetByteCount 方法來傳回編碼字元陣列所需的位元組數目。

using namespace System;
using namespace System::Text;
int main()
{
   
   // Unicode characters.
   
   // Pi
   // Sigma
   array<Char>^chars = {L'\u03a0',L'\u03a3',L'\u03a6',L'\u03a9'};
   UTF7Encoding^ utf7 = gcnew UTF7Encoding;
   int byteCount = utf7->GetByteCount( chars, 1, 2 );
   Console::WriteLine( "{0} bytes needed to encode characters.", byteCount );
}
using System;
using System.Text;

class UTF7EncodingExample {
    public static void Main() {
        // Unicode characters.
        Char[] chars = new Char[] {
            '\u0023', // #
            '\u0025', // %
            '\u03a0', // Pi
            '\u03a3'  // Sigma
        };

        UTF7Encoding utf7 = new UTF7Encoding();
        int byteCount = utf7.GetByteCount(chars, 1, 2);
        Console.WriteLine(
            "{0} bytes needed to encode characters.", byteCount
        );
    }
}
Imports System.Text
Imports Microsoft.VisualBasic.Strings

Class UTF7EncodingExample
    
    Public Shared Sub Main()
        ' Unicode characters.
        ' ChrW(35)  = #
        ' ChrW(37)  = %
        ' ChrW(928) = Pi
        ' ChrW(931) = Sigma
        Dim chars() As Char = {ChrW(35), ChrW(37), ChrW(928), ChrW(931)}
        
        Dim utf7 As New UTF7Encoding()
        Dim byteCount As Integer = utf7.GetByteCount(chars, 1, 2)
        Console.WriteLine("{0} bytes needed to encode characters.", byteCount)
    End Sub
End Class

備註

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

另請參閱

適用於

GetByteCount(Char*, Int32)

Source:
UTF7Encoding.cs
Source:
UTF7Encoding.cs
Source:
UTF7Encoding.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 (在 Visual Basic .NET 中為 Nothing)。

count 小於零。

-或-

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

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

-和-

EncoderFallback 設定為 EncoderExceptionFallback

備註

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

另請參閱

適用於

GetByteCount(Char[], Int32, Int32)

Source:
UTF7Encoding.cs
Source:
UTF7Encoding.cs
Source:
UTF7Encoding.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 (Nothing)。

indexcount 小於零。

-或-

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

-或-

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

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

-和-

EncoderFallback 設定為 EncoderExceptionFallback

範例

下列程式碼範例示範如何使用 GetByteCount 方法來傳回編碼 Unicode 字元陣列所需的位元組數目。

using namespace System;
using namespace System::Text;
int main()
{
   
   // Unicode characters.
   
   // Pi
   // Sigma
   array<Char>^chars = {L'\u03a0',L'\u03a3',L'\u03a6',L'\u03a9'};
   UTF7Encoding^ utf7 = gcnew UTF7Encoding;
   int byteCount = utf7->GetByteCount( chars, 1, 2 );
   Console::WriteLine( "{0} bytes needed to encode characters.", byteCount );
}
using System;
using System.Text;

class UTF7EncodingExample {
    public static void Main() {
        // Unicode characters.
        Char[] chars = new Char[] {
            '\u0023', // #
            '\u0025', // %
            '\u03a0', // Pi
            '\u03a3'  // Sigma
        };

        UTF7Encoding utf7 = new UTF7Encoding();
        int byteCount = utf7.GetByteCount(chars, 1, 2);
        Console.WriteLine(
            "{0} bytes needed to encode characters.", byteCount
        );
    }
}
Imports System.Text
Imports Microsoft.VisualBasic.Strings

Class UTF7EncodingExample
    
    Public Shared Sub Main()
        ' Unicode characters.
        ' ChrW(35)  = #
        ' ChrW(37)  = %
        ' ChrW(928) = Pi
        ' ChrW(931) = Sigma
        Dim chars() As Char = {ChrW(35), ChrW(37), ChrW(928), ChrW(931)}
        
        Dim utf7 As New UTF7Encoding()
        Dim byteCount As Integer = utf7.GetByteCount(chars, 1, 2)
        Console.WriteLine("{0} bytes needed to encode characters.", byteCount)
    End Sub
End Class

備註

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

另請參閱

適用於