指定した Unicode 文字配列から一連の文字をエンコードして得られた結果を格納するために必要なバイト数を計算します。
Overrides Overloads Public Function GetByteCount( _
ByVal chars() As Char, _ ByVal index As Integer, _ ByVal count As Integer _) As Integer
[C#]
public override int GetByteCount(char[] chars,intindex,intcount);
[C++]
public: int GetByteCount(__wchar_tchars __gc[],intindex,intcount);
[JScript]
public override function GetByteCount(
chars : Char[],index : int,count : int) : int;
パラメータ
- chars
エンコードする Unicode 文字配列。 - index
エンコードする chars 内の最初の文字のインデックス。 - count
エンコードする文字数。
戻り値
chars 内の index から index + count-1 までの範囲を ASCIIEncoding としてエンコードするために必要なバイト数。
例外
例外の種類 | 条件 |
---|---|
ArgumentNullException | chars が null 参照 (Visual Basic では Nothing) です。 |
ArgumentOutOfRangeException | index または count が 0 未満です。
または index に count を加算した値が、 chars の長さを超えています。 |
解説
エンコードされた文字を格納するために GetBytes メソッドによって要求された配列サイズを、正確に計算するには GetByteCount を使用し、最大値を計算するには GetMaxByteCount を使用します。
使用例
[Visual Basic, C#, C++] ASCIIEncoding を使用して Unicode 文字列の配列をエンコードするために必要なバイト数を、 GetByteCount メソッドを使用して返す方法を次の例に示します。
Imports System
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
[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
);
}
}
[C++]
#using <mscorlib.dll>
using namespace System;
using namespace System::Text;
int main()
{
// Unicode characters.
Char chars[] =
{
L'\u03a0', // Pi
L'\u03a3', // Sigma
L'\u03a6',
L'\u03a9'
};
ASCIIEncoding* ascii = new ASCIIEncoding();
int byteCount = ascii -> GetByteCount(chars, 1, 2);
Console::WriteLine(S" {0} bytes needed to encode characters.", byteCount.ToString());
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard
参照
ASCIIEncoding クラス | ASCIIEncoding メンバ | System.Text 名前空間 | ASCIIEncoding.GetByteCount オーバーロードの一覧