次の方法で共有


Encoding.GetCharCount メソッド

バイト配列をデコードして生成された文字数を計算します。

オーバーロードの一覧

バイト配列をデコードして生成された文字数を計算します。

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Public Overridable Function GetCharCount(Byte()) As Integer

[C#] public virtual int GetCharCount(byte[]);

[C++] public: virtual int GetCharCount(unsigned char __gc[]);

[JScript] public function GetCharCount(Byte[]) : int;

派生クラスでオーバーライドされた場合は、バイト配列内の指定した範囲の要素をデコードして生成された文字数を計算します。

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Public MustOverride Function GetCharCount(Byte(), Integer, Integer) As Integer

[C#] public abstract int GetCharCount(byte[], int, int);

[C++] public: virtual int GetCharCount(unsigned char __gc[], int, int) = 0;

[JScript] public abstract function GetCharCount(Byte[], int, int) : int;

使用例

[Visual Basic, C#, C++] メモ   ここでは、GetCharCount のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。

 
Imports System
Imports System.Text
Imports Microsoft.VisualBasic

Namespace Convert_Example
    Class MyConvertExampleClass
        Shared Sub Main()
            Dim unicodeString As String = "This string contains the unicode character Pi(" & ChrW(&H03A0) & ")"

            ' Create two different encodings.
            Dim ascii As Encoding = Encoding.ASCII
            Dim [unicode] As Encoding = Encoding.Unicode

            ' Convert the string into a byte[].
            Dim unicodeBytes As Byte() = [unicode].GetBytes(unicodeString)

            ' Perform the conversion from one encoding to the other.
            Dim asciiBytes As Byte() = Encoding.Convert([unicode], ascii, unicodeBytes)

            ' Convert the new byte[] into a char[] and then into a string.
            ' This is a slightly different approach to converting to illustrate
            ' the use of GetCharCount/GetChars.
            Dim asciiChars(ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)) As Char
            ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0)
            Dim asciiString As New String(asciiChars)

            ' Display the strings created before and after the conversion.
            Console.WriteLine("Original string: {0}", unicodeString)
            Console.WriteLine("Ascii converted string: {0}", asciiString)
        End Sub
    End Class
End Namespace

[C#] 
using System;
using System.Text;

namespace ConvertExample
{
   class ConvertExampleClass
   {
      static void Main()
      {
         string unicodeString = "This string contains the unicode character Pi(\u03a0)";

         // Create two different encodings.
         Encoding ascii = Encoding.ASCII;
         Encoding unicode = Encoding.Unicode;

         // Convert the string into a byte[].
         byte[] unicodeBytes = unicode.GetBytes(unicodeString);

         // Perform the conversion from one encoding to the other.
         byte[] asciiBytes = Encoding.Convert(unicode, ascii, unicodeBytes);
            
         // Convert the new byte[] into a char[] and then into a string.
         // This is a slightly different approach to converting to illustrate
         // the use of GetCharCount/GetChars.
         char[] asciiChars = new char[ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
         ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
         string asciiString = new string(asciiChars);

         // Display the strings created before and after the conversion.
         Console.WriteLine("Original string: {0}", unicodeString);
         Console.WriteLine("Ascii converted string: {0}", asciiString);
      }
   }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Text;

int main()
{
   String* unicodeString = S"This string contains the unicode character Pi(\u03a0)";

   // Create two different encodings.
   Encoding * ascii = Encoding::ASCII;
   Encoding * unicode = Encoding::Unicode;

   // Convert the string into a Byte->Item[].
   Byte unicodeBytes[] = unicode -> GetBytes(unicodeString);

   // Perform the conversion from one encoding to the other.
   Byte asciiBytes[] = Encoding::Convert(unicode, ascii, unicodeBytes);

   // Convert the new Byte into[] a char and[] then into a string.
   // This is a slightly different approach to converting to illustrate
   // the use of GetCharCount/GetChars.
   Char asciiChars[] = new Char[ascii -> GetCharCount(asciiBytes, 0, asciiBytes -> Length)];
   ascii -> GetChars(asciiBytes, 0, asciiBytes->Length, asciiChars, 0);
   String* asciiString = new String(asciiChars);

   // Display the strings created before and after the conversion.
   Console::WriteLine(S"Original String*: {0}", unicodeString);
   Console::WriteLine(S"Ascii converted String*: {0}", asciiString);
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

参照

Encoding クラス | Encoding メンバ | System.Text 名前空間