ASCIIEncoding.GetByteCount 方法

定义

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

重载

GetByteCount(ReadOnlySpan<Char>)

计算对指定字符范围进行编码所产生的字节数。

GetByteCount(String)

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

GetByteCount(Char*, Int32)

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

GetByteCount(Char[], Int32, Int32)

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

GetByteCount(ReadOnlySpan<Char>)

Source:
ASCIIEncoding.cs
Source:
ASCIIEncoding.cs
Source:
ASCIIEncoding.cs

计算对指定字符范围进行编码所产生的字节数。

C#
public override int GetByteCount (ReadOnlySpan<char> chars);

参数

chars
ReadOnlySpan<Char>

包含要编码的字符集的范围。

返回

对指定字符范围进行编码所产生的字节数。

注解

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

适用于

.NET 9 和其他版本
产品 版本
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9

GetByteCount(String)

Source:
ASCIIEncoding.cs
Source:
ASCIIEncoding.cs
Source:
ASCIIEncoding.cs

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

C#
public override int GetByteCount (string chars);

参数

chars
String

包含要编码的字符集的 String

返回

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

例外

charsnull

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

发生回退(有关详细信息,请参阅采用 .NET 的字符编码

-和-

EncoderFallback 设置为 EncoderExceptionFallback

示例

以下示例演示如何使用 GetByteCount 方法返回使用 ASCIIEncoding对字符串进行编码所需的字节数。

C#
using System;
using System.Text;

class ASCIIEncodingExample {
    public static void Main() {
        String chars = "ASCII Encoding Example";

        ASCIIEncoding ascii = new ASCIIEncoding();
        int byteCount = ascii.GetByteCount(chars);
        Console.WriteLine(
            "{0} bytes needed to encode string.", byteCount
        );
    }
}

注解

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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

GetByteCount(Char*, Int32)

Source:
ASCIIEncoding.cs
Source:
ASCIIEncoding.cs
Source:
ASCIIEncoding.cs

重要

此 API 不符合 CLS。

符合 CLS 的替代方案
System.Text.ASCIIEncoding.GetByteCount(Char[], Int32, Int32)

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

C#
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
public override int GetByteCount (char* chars, int count);
C#
[System.CLSCompliant(false)]
public override int GetByteCount (char* chars, int count);
C#
[System.CLSCompliant(false)]
[System.Runtime.InteropServices.ComVisible(false)]
public override int GetByteCount (char* chars, int count);
C#
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
[System.Runtime.InteropServices.ComVisible(false)]
public override int GetByteCount (char* chars, int count);

参数

chars
Char*

指向第一个要编码的字符的指针。

count
Int32

要编码的字符的数目。

返回

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

属性

例外

charsnull

count 小于零。

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

发生回退(有关详细信息,请参阅采用 .NET 的字符编码

-和-

EncoderFallback 设置为 EncoderExceptionFallback

注解

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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

GetByteCount(Char[], Int32, Int32)

Source:
ASCIIEncoding.cs
Source:
ASCIIEncoding.cs
Source:
ASCIIEncoding.cs

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

C#
public override int GetByteCount (char[] chars, int index, int count);

参数

chars
Char[]

包含要编码的字符集的字符数组。

index
Int32

第一个要编码的字符的索引。

count
Int32

要编码的字符的数目。

返回

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

例外

charsnull

indexcount 小于零。

indexcount 不表示 chars中的有效范围。

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

发生回退(有关详细信息,请参阅采用 .NET 的字符编码

-和-

EncoderFallback 设置为 EncoderExceptionFallback

示例

以下示例演示如何使用 GetByteCount 方法返回使用 ASCIIEncoding对 Unicode 字符数组进行编码所需的字节数。

C#
using System;
using System.Text;

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

        ASCIIEncoding ascii = new ASCIIEncoding();
        int byteCount = ascii.GetByteCount(chars, 1, 2);
        Console.WriteLine(
            "{0} bytes needed to encode characters.", byteCount
        );
    }
}

注解

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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0