ASCIIEncoding.GetCharCount 方法

定义

计算对一个字节序列进行解码所产生的字符数。

重载

GetCharCount(Byte*, Int32)

计算对一个字节序列(从指定的字节指针开始)进行解码所产生的字符数。

GetCharCount(Byte[], Int32, Int32)

计算对指定字节数组中的一个字节序列进行解码所产生的字符数。

GetCharCount(ReadOnlySpan<Byte>)

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

GetCharCount(Byte*, Int32)

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

重要

此 API 不符合 CLS。

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

计算对一个字节序列(从指定的字节指针开始)进行解码所产生的字符数。

public:
 override int GetCharCount(System::Byte* bytes, int count);
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
public override int GetCharCount (byte* bytes, int count);
[System.CLSCompliant(false)]
public override int GetCharCount (byte* bytes, int count);
[System.CLSCompliant(false)]
[System.Runtime.InteropServices.ComVisible(false)]
public override int GetCharCount (byte* bytes, int count);
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
[System.Runtime.InteropServices.ComVisible(false)]
public override int GetCharCount (byte* bytes, int count);
[<System.CLSCompliant(false)>]
[<System.Security.SecurityCritical>]
override this.GetCharCount : nativeptr<byte> * int -> int
[<System.CLSCompliant(false)>]
override this.GetCharCount : nativeptr<byte> * int -> int
[<System.CLSCompliant(false)>]
[<System.Runtime.InteropServices.ComVisible(false)>]
override this.GetCharCount : nativeptr<byte> * int -> int
[<System.CLSCompliant(false)>]
[<System.Security.SecurityCritical>]
[<System.Runtime.InteropServices.ComVisible(false)>]
override this.GetCharCount : nativeptr<byte> * int -> int

参数

bytes
Byte*

指向第一个要解码的字节的指针。

count
Int32

要解码的字节数。

返回

对指定字节序列进行解码所产生的字符数。

属性

例外

bytes 上声明的默认值为 null

count 小于零。

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

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

-和-

DecoderFallback 设置为 DecoderExceptionFallback

注解

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

另请参阅

适用于

GetCharCount(Byte[], Int32, Int32)

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

计算对指定字节数组中的一个字节序列进行解码所产生的字符数。

public:
 override int GetCharCount(cli::array <System::Byte> ^ bytes, int index, int count);
public override int GetCharCount (byte[] bytes, int index, int count);
override this.GetCharCount : byte[] * int * int -> int
Public Overrides Function GetCharCount (bytes As Byte(), index As Integer, count As Integer) As Integer

参数

bytes
Byte[]

包含要解码的字节序列的字节数组。

index
Int32

第一个要解码的字节的索引。

count
Int32

要解码的字节数。

返回

对指定字节序列进行解码所产生的字符数。

例外

bytesnull

indexcount 小于零。

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

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

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

-和-

DecoderFallback 设置为 DecoderExceptionFallback

示例

以下示例演示如何使用 GetCharCount 方法返回通过解码字节数组中的元素范围而生成的字符数。

using namespace System;
using namespace System::Text;
int main()
{
   array<Byte>^bytes = {65,83,67,73,73,32,69,110,99,111,100,105,110,103,32,69,120,97,109,112,108,101};
   ASCIIEncoding^ ascii = gcnew ASCIIEncoding;
   int charCount = ascii->GetCharCount( bytes, 6, 8 );
   Console::WriteLine( "{0} characters needed to decode bytes.", charCount );
}
using System;
using System.Text;

class ASCIIEncodingExample {
    public static void Main() {
        Byte[] bytes = new Byte[] {
             65,  83,  67,  73,  73,  32,  69,
            110,  99, 111, 100, 105, 110, 103,
             32,  69, 120,  97, 109, 112, 108, 101
        };

        ASCIIEncoding ascii = new ASCIIEncoding();
        int charCount = ascii.GetCharCount(bytes, 6, 8);
        Console.WriteLine(
            "{0} characters needed to decode bytes.", charCount
        );
    }
}
Imports System.Text

Class ASCIIEncodingExample
    Public Shared Sub Main()
        Dim bytes() As Byte = { _
             65,  83,  67,  73,  73,  32,  69, _
            110,  99, 111, 100, 105, 110, 103, _
             32,  69, 120,  97, 109, 112, 108, 101}
      
        Dim ascii As New ASCIIEncoding()
        Dim charCount As Integer = ascii.GetCharCount(bytes, 6, 8)
        Console.WriteLine("{0} characters needed to decode bytes.", charCount)
    End Sub
End Class

注解

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

另请参阅

适用于

GetCharCount(ReadOnlySpan<Byte>)

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

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

public:
 override int GetCharCount(ReadOnlySpan<System::Byte> bytes);
public override int GetCharCount (ReadOnlySpan<byte> bytes);
override this.GetCharCount : ReadOnlySpan<byte> -> int
Public Overrides Function GetCharCount (bytes As ReadOnlySpan(Of Byte)) As Integer

参数

bytes
ReadOnlySpan<Byte>

包含要解码的字节集的范围。

返回

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

注解

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

适用于