ASCIIEncoding.GetByteCount メソッド

定義

文字のセットをエンコードすることによって生成されるバイト数を計算します。

オーバーロード

GetByteCount(ReadOnlySpan<Char>)

指定された文字スパンをエンコードすることによって生成されるバイト数を計算します。

GetByteCount(String)

指定した String 内の文字をエンコードすることによって生成されるバイト数を計算します。

GetByteCount(Char*, Int32)

指定した文字ポインターで始まる文字のセットをエンコードすることによって生成されるバイト数を計算します。

GetByteCount(Char[], Int32, Int32)

指定した文字配列から文字のセットをエンコードすることによって生成されるバイト数を計算します。

GetByteCount(ReadOnlySpan<Char>)

ソース:
ASCIIEncoding.cs
ソース:
ASCIIEncoding.cs
ソース:
ASCIIEncoding.cs

指定された文字スパンをエンコードすることによって生成されるバイト数を計算します。

public:
 override int GetByteCount(ReadOnlySpan<char> chars);
public override int GetByteCount (ReadOnlySpan<char> chars);
override this.GetByteCount : ReadOnlySpan<char> -> int
Public Overrides Function GetByteCount (chars As ReadOnlySpan(Of Char)) As Integer

パラメーター

chars
ReadOnlySpan<Char>

エンコードする文字のセットを格納するスパン。

戻り値

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

注釈

結果のバイトを格納するために 必要な GetBytes 正確なサイズを計算するには、 を使用します GetByteCount。 最大サイズを計算するには、 を使用します GetMaxByteCount。 メソッドは一般に、より GetByteCount 少ないメモリの割り当てを可能にしますが、 GetMaxByteCount メソッドは通常、より高速に実行されます。

適用対象

GetByteCount(String)

ソース:
ASCIIEncoding.cs
ソース:
ASCIIEncoding.cs
ソース:
ASCIIEncoding.cs

指定した String 内の文字をエンコードすることによって生成されるバイト数を計算します。

public:
 override int GetByteCount(System::String ^ chars);
public override int GetByteCount (string chars);
override this.GetByteCount : string -> int
Public Overrides Function GetByteCount (chars As String) As Integer

パラメーター

chars
String

エンコード対象の文字のセットを格納している String

戻り値

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

例外

charsnullです。

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

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

および

EncoderFallbackEncoderExceptionFallback に設定されます。

次の例では、 メソッドを使用して、 を GetByteCount 使用して文字列をエンコードするために必要なバイト数を返す方法を ASCIIEncoding示します。

using namespace System;
using namespace System::Text;
int main()
{
   String^ chars = "ASCII Encoding Example";
   ASCIIEncoding^ ascii = gcnew ASCIIEncoding;
   int byteCount = ascii->GetByteCount( chars );
   Console::WriteLine( " {0} bytes needed to encode string.", byteCount );
}
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
        );
    }
}
Imports System.Text

Class ASCIIEncodingExample
    Public Shared Sub Main()
        Dim chars As String = "ASCII Encoding Example"

        Dim ascii As New ASCIIEncoding()
        Dim byteCount As Integer = ascii.GetByteCount(chars)
        Console.WriteLine("{0} bytes needed to encode string.", byteCount)
    End Sub
End Class

注釈

結果のバイトを格納するために に GetBytes 必要な配列サイズを正確に計算するために、アプリケーションでは を使用 GetByteCountします。 配列の最大サイズを計算するには、アプリケーションで を使用 GetMaxByteCountする必要があります。 メソッドは一般に、より GetByteCount 少ないメモリの割り当てを可能にしますが、 GetMaxByteCount メソッドは通常、より高速に実行されます。

こちらもご覧ください

適用対象

GetByteCount(Char*, Int32)

ソース:
ASCIIEncoding.cs
ソース:
ASCIIEncoding.cs
ソース:
ASCIIEncoding.cs

重要

この API は CLS 準拠ではありません。

CLS 準拠の代替
System.Text.ASCIIEncoding.GetByteCount(Char[], Int32, Int32)

指定した文字ポインターで始まる文字のセットをエンコードすることによって生成されるバイト数を計算します。

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

エンコードする文字数。

戻り値

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

属性

例外

charsnullです。

count が 0 未満です。

または

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

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

および

EncoderFallbackEncoderExceptionFallback に設定されます。

注釈

結果のバイトを格納するために に GetBytes 必要な配列サイズを正確に計算するために、アプリケーションでは を使用 GetByteCountします。 配列の最大サイズを計算するには、アプリケーションで を使用 GetMaxByteCountする必要があります。 メソッドは一般に、より GetByteCount 少ないメモリの割り当てを可能にしますが、 GetMaxByteCount メソッドは通常、より高速に実行されます。

こちらもご覧ください

適用対象

GetByteCount(Char[], Int32, Int32)

ソース:
ASCIIEncoding.cs
ソース:
ASCIIEncoding.cs
ソース:
ASCIIEncoding.cs

指定した文字配列から文字のセットをエンコードすることによって生成されるバイト数を計算します。

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

エンコードする文字数。

戻り値

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

例外

charsnullです。

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

または

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

または

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

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

および

EncoderFallbackEncoderExceptionFallback に設定されます。

次の例では、 メソッドを使用して、 を GetByteCount 使用して Unicode 文字の配列をエンコードするために必要なバイト数を ASCIIEncoding返す方法を示します。

using namespace System;
using namespace System::Text;
int main()
{
   
   // Unicode characters.
   
   // Pi
   // Sigma
   array<Char>^chars = {L'\u03a0',L'\u03a3',L'\u03a6',L'\u03a9'};
   ASCIIEncoding^ ascii = gcnew ASCIIEncoding;
   int byteCount = ascii->GetByteCount( chars, 1, 2 );
   Console::WriteLine( " {0} bytes needed to encode characters.", byteCount.ToString() );
}
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
        );
    }
}
Imports System.Text
Imports Microsoft.VisualBasic.Strings

Class ASCIIEncodingExample
    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 ascii As New ASCIIEncoding()
        Dim byteCount As Integer = ascii.GetByteCount(chars, 1, 2)
        Console.WriteLine("{0} bytes needed to encode characters.", byteCount)
    End Sub
End Class

注釈

結果のバイトを格納するために に GetBytes 必要な配列サイズを正確に計算するために、アプリケーションでは を使用 GetByteCountします。 配列の最大サイズを計算するには、アプリケーションで を使用 GetMaxByteCountする必要があります。 メソッドは一般に、より GetByteCount 少ないメモリの割り当てを可能にしますが、 GetMaxByteCount メソッドは通常、より高速に実行されます。

こちらもご覧ください

適用対象