指定した 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 までの範囲をエンコードするために必要なバイト数。
例外
例外の種類 | 条件 |
---|---|
ArgumentNullException | chars が null 参照 (Visual Basic では Nothing) です。 |
ArgumentOutOfRangeException | index または count が 0 未満です。
または index に count を加算した値が、 chars の長さを超えています。 または 戻り値がオーバーフローしました。 |
ArgumentException | chars には、無効な文字のシーケンスが含まれ、無効なバイトが検出された場合に例外をスローするように指定されたこのインスタンスの UTF8Encoding コンストラクタも含まれています。 |
解説
エンコードされた文字を格納するために GetBytes メソッドによって要求された配列サイズを正確に計算するには GetByteCount を使用し、その最大値を計算するには GetMaxByteCount を使用します。
エラー検出がオフのときに、無効なサロゲート シーケンスが検出された場合、無効な文字は無視され、戻り値に影響を与えず、例外もスローされません。
使用例
[Visual Basic, C#, C++] UTF8Encoding を使用して Unicode 文字 chars
の配列をエンコードするために必要なバイト数を、 GetByteCount メソッドを使用して返す方法を次の例に示します。
Imports System
Imports System.Text
Imports Microsoft.VisualBasic.Strings
Class UTF8EncodingExample
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 utf8 As New UTF8Encoding()
Dim byteCount As Integer = utf8.GetByteCount(chars, 1, 2)
Console.WriteLine("{0} bytes needed to encode characters.", byteCount)
End Sub 'Main
End Class 'UTF8EncodingExample
[C#]
using System;
using System.Text;
class UTF8EncodingExample {
public static void Main() {
// Unicode characters.
Char[] chars = new Char[] {
'\u0023', // #
'\u0025', // %
'\u03a0', // Pi
'\u03a3' // Sigma
};
UTF8Encoding utf8 = new UTF8Encoding();
int byteCount = utf8.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'
};
UTF8Encoding* utf8 = new UTF8Encoding();
int byteCount = utf8 -> GetByteCount(chars, 1, 2);
Console::WriteLine(S"{0} bytes needed to encode characters.", __box(byteCount));
}
[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
参照
UTF8Encoding クラス | UTF8Encoding メンバ | System.Text 名前空間 | UTF8Encoding.GetByteCount オーバーロードの一覧