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です。

index または count が 0 未満です。

または

index および countbytesにおいて有効な範囲を表していません。

または

結果のバイト数が、整数として返すことのできる最大数を超えています。

エラーの検出が有効になり、bytes に無効なバイト シーケンスが含まれています。

フォールバックが発生しました (詳細については「.NET での文字エンコード」を参照)

および

DecoderFallbackDecoderExceptionFallback に設定されます。

次の例では、 メソッドを 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

デコードするバイト数。

戻り値

指定したバイト シーケンスをデコードすることによって生成される文字数。

属性

例外

bytesnullです。

count が 0 未満です。

または

結果のバイト数が、整数として返すことのできる最大数を超えています。

エラーの検出が有効になり、bytes に無効なバイト シーケンスが含まれています。

フォールバックが発生しました (詳細については「.NET での文字エンコード」を参照)

および

DecoderFallbackDecoderExceptionFallback に設定されます。

注釈

結果の文字を格納するために 必要な 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 スローされます。 エラー検出がないと、無効なシーケンスは無視され、例外はスローされません。

適用対象