UTF7Encoding.GetByteCount 方法

定义

计算对一组字符进行编码时产生的字节数。

重载

GetByteCount(String)

计算对指定 String 对象中的字符进行编码时产生的字节数。

GetByteCount(Char*, Int32)

计算对从指定的字符指针开始的一组字符进行编码时产生的字节数。

GetByteCount(Char[], Int32, Int32)

计算对指定字符数组中的一组字符进行编码时产生的字节数。

GetByteCount(String)

计算对指定 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 对象。

返回

Int32

对指定字符进行编码后生成的字节数。

属性

例外

snull (Nothing)。

得到的字节数大于可作为 int 返回的最大数量。

(发生回退,有关详细信息,请参阅 .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。 若要计算最大数组大小,应用程序应使用 GetMaxByteCountGetByteCount方法通常允许分配较少的内存,而 GetMaxByteCount 方法的执行速度通常更快。

另请参阅

适用于

GetByteCount(Char*, Int32)

重要

此 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

要编码的字符的数目。

返回

Int32

对指定字符进行编码后生成的字节数。

属性

例外

charsnull(在 Visual Basic .NET 中为 Nothing)。

count 小于零。

  • 或 - 得到的字节数大于可作为 int 返回的最大数量。

发生回退(有关详细信息,请参阅采用 .NET 的字符编码) -和- 将 EncoderFallback 设置为 EncoderExceptionFallback

注解

若要计算存储生成的字节所需的确切数组大小 GetBytes ,应用程序使用 GetByteCount。 若要计算最大数组大小,应用程序应使用 GetMaxByteCountGetByteCount方法通常允许分配较少的内存,而 GetMaxByteCount 方法的执行速度通常更快。

另请参阅

适用于

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中的有效范围。

  • 或 - 得到的字节数大于可作为 int 返回的最大数量。

发生回退(有关详细信息,请参阅采用 .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。 若要计算最大数组大小,应用程序应使用 GetMaxByteCountGetByteCount方法通常允许分配较少的内存,而 GetMaxByteCount 方法的执行速度通常更快。

另请参阅

适用于