UnicodeEncoding.GetByteCount メソッド

定義

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

オーバーロード

GetByteCount(String)

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

GetByteCount(Char*, Int32)

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

GetByteCount(Char[], Int32, Int32)

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

GetByteCount(String)

Source:
UnicodeEncoding.cs
Source:
UnicodeEncoding.cs
Source:
UnicodeEncoding.cs

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

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

パラメーター

s
String

エンコード対象の文字のセットを格納する文字列。

戻り値

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

例外

snullです。

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

エラーの検出が有効になり、s に無効な文字のシーケンスが含まれています。

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

および

EncoderFallbackEncoderExceptionFallback に設定されます。

次の例では、 GetMaxByteCount メソッドと GetByteCount(String) メソッドを呼び出して、文字列のエンコードに必要な最大バイト数と実際のバイト数を計算します。 また、バイト ストリームを格納するために必要な実際のバイト数もバイト順マークと共に表示されます。

using System;
using System.Text;

class UTF8EncodingExample {
    public static void Main() {
        String chars = "UTF-16 Encoding Example";
        Encoding unicode = Encoding.Unicode;

        Console.WriteLine("Bytes needed to encode '{0}':", chars);
        Console.WriteLine("   Maximum:         {0}",
                          unicode.GetMaxByteCount(chars.Length));
        Console.WriteLine("   Actual:          {0}",
                          unicode.GetByteCount(chars));
        Console.WriteLine("   Actual with BOM: {0}",
                          unicode.GetByteCount(chars) + unicode.GetPreamble().Length);
    }
}
// The example displays the following output:
//       Bytes needed to encode 'UTF-16 Encoding Example':
//          Maximum:         48
//          Actual:          46
//          Actual with BOM: 48
Imports System.Text

Module Example
    Public Sub Main()
        Dim chars As String = "UTF-16 Encoding Example"
        Dim unicode As Encoding = Encoding.Unicode

        Console.WriteLine("Bytes needed to encode '{0}':", chars)
        Console.WriteLine("   Maximum:         {0}",
                          unicode.GetMaxByteCount(chars.Length))
        Console.WriteLine("   Actual:          {0}",
                          unicode.GetByteCount(chars))
        Console.WriteLine("   Actual with BOM: {0}",
                          unicode.GetByteCount(chars) + unicode.GetPreamble().Length)
    End Sub
End Module
' The example displays the following output:
'       Bytes needed to encode 'UTF-16 Encoding Example':
'          Maximum:         48
'          Actual:          46
'          Actual with BOM: 48

注釈

結果のバイトを格納するために 必要 GetBytes な配列サイズを正確に計算するには、 メソッドを GetByteCount 呼び出します。 配列の最大サイズを計算するには、 メソッドを呼び出します GetMaxByteCount 。 メソッドは通常、割り当てるメモリが少なくなりますが、メソッドの実行速度は GetByteCount 一般に GetMaxByteCount 速くなります。

エラー検出では、無効なシーケンスにより、このメソッドは を ArgumentExceptionスローします。 エラー検出がないと、無効なシーケンスは無視され、例外はスローされません。

重要

エンコードされたバイトがファイルまたはストリームとして保存されるときに正しくデコードされるようにするには、エンコードされたバイトのストリームの前にプリアンブルを付けます。 バイト ストリームの先頭 (ファイルに書き込まれる一連のバイトの先頭など) にプリアンブルを挿入することは開発者の責任であり、プリアンブル内のバイト数は メソッドによって GetByteCount(String) 返される値には反映されません。

こちらもご覧ください

適用対象

GetByteCount(Char*, Int32)

Source:
UnicodeEncoding.cs
Source:
UnicodeEncoding.cs
Source:
UnicodeEncoding.cs

重要

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

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

public:
 override int GetByteCount(char* chars, int count);
[System.CLSCompliant(false)]
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)]
[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)>]
override this.GetByteCount : nativeptr<char> * int -> int
[<System.CLSCompliant(false)>]
[<System.Security.SecurityCritical>]
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 未満です。

または

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

エラー検出が有効であり、chars に無効な文字シーケンスが含まれています。

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

および

EncoderFallbackEncoderExceptionFallback に設定されます。

注釈

結果のバイトを格納する必要がある GetBytes 配列サイズを正確に計算するには、 メソッドを呼び出します GetByteCount 。 配列の最大サイズを計算するには、 メソッドを呼び出します GetMaxByteCount 。 メソッドは通常、割り当てるメモリが少なくなりますが、メソッドの実行速度は GetByteCount 一般に GetMaxByteCount 速くなります。

エラー検出では、無効なシーケンスにより、このメソッドは を ArgumentExceptionスローします。 エラー検出がないと、無効なシーケンスは無視され、例外はスローされません。

重要

エンコードされたバイトがファイルまたはストリームとして保存されるときに正しくデコードされるようにするには、エンコードされたバイトのストリームの前にプリアンブルを付けます。 バイト ストリームの先頭 (ファイルに書き込まれる一連のバイトの先頭など) にプリアンブルを挿入することは開発者の責任であり、プリアンブル内のバイト数は メソッドによって GetByteCount 返される値には反映されません。

こちらもご覧ください

適用対象

GetByteCount(Char[], Int32, Int32)

Source:
UnicodeEncoding.cs
Source:
UnicodeEncoding.cs
Source:
UnicodeEncoding.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 (Nothing) です。

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

または

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

または

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

エラーの検出が有効になり、chars に無効な文字のシーケンスが含まれています。

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

および

EncoderFallbackEncoderExceptionFallback に設定されます。

次の例では、配列に英大文字と小文字を設定し、 メソッドを GetByteCount(Char[], Int32, Int32) 呼び出して、ラテン小文字をエンコードするために必要なバイト数を決定します。 その後、バイト順マークが追加された場合に必要な合計バイト数と共に、この情報が表示されます。 この数値を メソッドによって GetMaxByteCount 返される値と比較します。これは、ラテン小文字をエンコードするために必要な最大バイト数を示します。 次の例では、ギリシャ文字とキリル文字の組み合わせを配列に設定し、 メソッドを GetByteCount(Char[], Int32, Int32) 呼び出して、キリル文字のエンコードに必要なバイト数を決定します。 その後、バイト順マークが追加された場合に必要な合計バイト数と共に、この情報が表示されます。 この数値を、 メソッドによって GetMaxByteCount 返される値と比較します。これは、キリル文字のエンコードに必要な最大バイト数を示します。

using System;
using System.Text;

public class Example
{
   public static void Main()
   {
      int uppercaseStart = 0x0041;
      int uppercaseEnd = 0x005a;
      int lowercaseStart = 0x0061;
      int lowercaseEnd = 0x007a;
      // Instantiate a UTF8 encoding object with BOM support.
      Encoding unicode = Encoding.Unicode;

      // Populate array with characters.
      char[] chars = new char[lowercaseEnd - lowercaseStart + uppercaseEnd - uppercaseStart + 2];
      int index = 0;
      for (int ctr = uppercaseStart; ctr <= uppercaseEnd; ctr++) {
         chars[index] = (char)ctr;
         index++;
      }
      for (int ctr = lowercaseStart; ctr <= lowercaseEnd; ctr++) {
         chars[index] = (char)ctr;
         index++;
      }

      // Display the bytes needed for the lowercase characters.
      Console.WriteLine("Bytes needed for lowercase Latin characters:");
      Console.WriteLine("   Maximum:         {0,5:N0}",
                        unicode.GetMaxByteCount(lowercaseEnd - lowercaseStart + 1));
      Console.WriteLine("   Actual:          {0,5:N0}",
                        unicode.GetByteCount(chars, uppercaseEnd - uppercaseStart + 1,
                                          lowercaseEnd - lowercaseStart + 1));
      Console.WriteLine("   Actual with BOM: {0,5:N0}",
                        unicode.GetByteCount(chars, uppercaseEnd - uppercaseStart + 1,
                                          lowercaseEnd - lowercaseStart + 1) +
                                          unicode.GetPreamble().Length);
   }
}
// The example displays the following output:
//       Bytes needed for lowercase Latin characters:
//          Maximum:            54
//          Actual:             52
//          Actual with BOM:    54
Imports System.Text

Module Example
   Public Sub Main()
      Dim uppercaseStart As Integer = &h0041
      Dim uppercaseEnd As Integer = &h005a
      Dim lowercaseStart As Integer = &h0061
      Dim lowercaseEnd As Integer = &h007a
      ' Instantiate a UTF8 encoding object with BOM support.
      Dim unicode As Encoding = Encoding.Unicode
      
      ' Populate array with characters.
      Dim chars(lowercaseEnd - lowercaseStart + uppercaseEnd - uppercaseStart + 1) As Char
      Dim index As Integer = 0
      For ctr As Integer = uppercaseStart To uppercaseEnd
         chars(index) = ChrW(ctr)
         index += 1
      Next
      For ctr As Integer = lowercaseStart To lowercaseEnd
         chars(index) = ChrW(ctr)
         index += 1
      Next

      ' Display the bytes needed for the lowercase characters.
        Console.WriteLine("Bytes needed for lowercase Latin characters:")
        Console.WriteLine("   Maximum:         {0,5:N0}",
                          unicode.GetMaxByteCount(lowercaseEnd - lowercaseStart + 1))
        Console.WriteLine("   Actual:          {0,5:N0}",
                          unicode.GetByteCount(chars, uppercaseEnd - uppercaseStart + 1,
                                            lowercaseEnd - lowercaseStart + 1))
        Console.WriteLine("   Actual with BOM: {0,5:N0}",
                          unicode.GetByteCount(chars, uppercaseEnd - uppercaseStart + 1,
                                            lowercaseEnd - lowercaseStart + 1) +
                                            unicode.GetPreamble().Length)
   End Sub
End Module
' The example displays the following output:
'       Bytes needed for lowercase Latin characters:
'          Maximum:            54
'          Actual:             52
'          Actual with BOM:    54

注釈

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

エラー検出が有効になっている場合、無効なシーケンスにより、このメソッドは をスローします ArgumentException。 エラー検出がないと、無効なシーケンスは無視され、例外はスローされません。

エンコードされたバイトがファイルまたはストリームとして保存されるときに正しくデコードされるようにするには、エンコードされたバイトのストリームの前にプリアンブルを付けます。 バイト ストリームの先頭 (ファイルに書き込まれる一連のバイトの先頭など) にプリアンブルを挿入することは開発者の責任であり、preamble のバイト数は メソッドによって GetByteCount(Char[], Int32, Int32) 返される値に反映されません。

こちらもご覧ください

適用対象