UTF8Encoding.GetCharCount 方法

定义

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

重载

GetCharCount(Byte[], Int32, Int32)

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

GetCharCount(Byte*, Int32)

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

GetCharCount(ReadOnlySpan<Byte>)

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

GetCharCount(Byte[], Int32, Int32)

Source:
UTF8Encoding.cs
Source:
UTF8Encoding.cs
Source:
UTF8Encoding.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中的有效范围。

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

启用了错误检测,并且 bytes 包含无效的字节序列。

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

-和-

DecoderFallback 设置为 DecoderExceptionFallback

示例

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

using namespace System;
using namespace System::Text;
int main()
{
   array<Byte>^bytes = {85,84,70,56,32,69,110,99,111,100,105,110,103,32,69,120,97,109,112,108,101};
   UTF8Encoding^ utf8 = gcnew UTF8Encoding;
   int charCount = utf8->GetCharCount( bytes, 2, 8 );
   Console::WriteLine( "{0} characters needed to decode bytes.", charCount );
}
using System;
using System.Text;

class UTF8EncodingExample {
    public static void Main() {
        Byte[] bytes = new Byte[] {
             85,  84,  70,  56,  32,  69, 110,
             99, 111, 100, 105, 110, 103,  32,
             69, 120,  97, 109, 112, 108, 101
        };

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

Class UTF8EncodingExample
    
    Public Shared Sub Main()
        Dim bytes() As Byte = { _
            85,  84,  70,  56,  32,  69, 110, _
            99, 111, 100, 105, 110, 103,  32, _
            69, 120,  97, 109, 112, 108, 101 _
        }
        
        Dim utf8 As New UTF8Encoding()
        Dim charCount As Integer = utf8.GetCharCount(bytes, 2, 8)
        Console.WriteLine("{0} characters needed to decode bytes.", charCount)
    End Sub
End Class

注解

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

使用错误检测时,无效序列会导致此方法引发 ArgumentException 异常。 如果不进行错误检测,将忽略无效序列,并且不会引发异常。

另请参阅

适用于

GetCharCount(Byte*, Int32)

Source:
UTF8Encoding.cs
Source:
UTF8Encoding.cs
Source:
UTF8Encoding.cs

重要

此 API 不符合 CLS。

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

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 小于零。

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

启用了错误检测,并且 bytes 包含无效的字节序列。

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

-和-

DecoderFallback 设置为 DecoderExceptionFallback

注解

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

使用错误检测时,无效序列会导致此方法引发 ArgumentException 异常。 如果不进行错误检测,将忽略无效序列,并且不会引发异常。

另请参阅

适用于

GetCharCount(ReadOnlySpan<Byte>)

Source:
UTF8Encoding.cs
Source:
UTF8Encoding.cs
Source:
UTF8Encoding.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 调用 方法。 若要计算最大大小,请 GetMaxCharCount 调用 方法。 方法 GetCharCount 通常分配较少的内存,而 GetMaxCharCount 方法的执行速度通常更快。

使用错误检测时,无效序列会导致此方法引发 ArgumentException 异常。 如果不进行错误检测,将忽略无效序列,并且不会引发异常。

适用于