クリックして評価とフィードバックをお寄せください
MSDN
MSDN ライブラリ
.NET 開発
以前のバージョン
.NET Framework SDK 2.0
System.Text
Encoding クラス
すべて縮小/すべて展開 すべて縮小
このページは次のバージョンについて記述しています。
Microsoft Visual Studio 2005/.NET Framework 2.0

その他のバージョンについては、以下の情報を参照してください。
Encoding クラス
文字エンコーディングを表します。

名前空間: System.Text
アセンブリ: mscorlib (mscorlib.dll 内)

Visual Basic (宣言)
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public MustInherit Class Encoding
	Implements ICloneable
Visual Basic (使用法)
Dim instance As Encoding
C#
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public abstract class Encoding : ICloneable
C++
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public ref class Encoding abstract : ICloneable
J#
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public abstract class Encoding implements ICloneable
JScript
SerializableAttribute 
ComVisibleAttribute(true) 
public abstract class Encoding implements ICloneable
XAML
適用できません。

エンコーディングは、Unicode 文字のセットをバイト シーケンスに変換するプロセスです。一方、デコードは、エンコードされたバイト シーケンスを Unicode 文字のセットに変換するプロセスです。このクラスは、バイト配列などの任意のバイナリ データではなく、Unicode 文字を操作するためのものです。アプリケーションで任意のバイナリ データをテキストにエンコードする必要がある場合は、System.Convert.ToBase64CharArray などのメソッドで実装されている UUencode などのプロトコルを使用する必要があります。

Unicode Standard では、サポートされるすべてのスクリプトについて、各文字にコード ポイントの数値を割り当てています。コード ポイントのエンコードには UTF (Unicode Transformation Format) が使用されます。Unicode Standard バージョン 3.2 では、次の UTF が使用されています。

  • UTF-8。各コード ポイントが、1 バイトから 4 バイトのシーケンスとして表現されます。

  • UTF-16。各コード ポイントが、1 つまたは 2 つの 16 ビット整数のシーケンスとして表現されます。

  • UTF-32。各コード ポイントが 32 ビット整数として表現されます。

注意に関するメモ注意 :

Unicode 以外の多くのエンコーディングではレガシ アプリケーションとの互換性がサポートされていますが、選択できる場合は Unicode エンコーディング (UTF8EncodingUnicodeEncoding、および UTF32Encoding) をお勧めします。セキュリティ上の理由から、エンコーディングを使用するときは、エラー検出を有効にすることや、GetEncoding などのメソッドに用意されている encoderFallback 値や decoderFallback 値を明示的に指定することをアプリケーションで検討する必要があります。既定の動作は "最もふさわしい" フォールバックであり、分音記号の欠落など、予期しない情報の損失が発生する可能性があります。最もふさわしいフォールバックを使用した場合は、予期しない悪用の原因となるセキュリティの脆弱性につながる可能性もあります。

.NET Framework では、現在の Unicode エンコーディング、および他のエンコーディングをサポートするため、次のような Encoding クラスを実装しています。

  • ASCIIEncoding は、Unicode 文字を 1 個の 7 ビット ASCII 文字としてエンコードします。このエンコーディングは、U+0000 から U+007F までの文字値だけをサポートします。コード ページは 20127 です。ASCII プロパティを通じても使用できます。

  • UTF7Encoding は、UTF-7 エンコーディングを使用して Unicode 文字をエンコードします。このエンコーディングは、すべての Unicode 文字値をサポートしています。コード ページは 65000 です。UTF7 プロパティを通じても使用できます。

  • UTF8Encoding は、UTF-8 エンコーディングを使用して Unicode 文字をエンコードします。このエンコーディングは、すべての Unicode 文字値をサポートしています。コード ページは 65001 です。UTF8 プロパティを通じて使用できます。

  • UnicodeEncoding は、UTF-16 エンコーディングを使用して Unicode 文字をエンコードします。リトル エンディアン (コード ページ 1200) とビッグ エンディアン (コード ページ 1201) の両方のバイト順をサポートしています。Unicode プロパティ、および BigEndianUnicode プロパティを通じても使用できます。

  • UTF32Encoding は、UTF-32 エンコーディングを使用して Unicode 文字をエンコードします。リトル エンディアン (コード ページ 65005) とビッグ エンディアン (コード ページ 65006) の両方のバイト順をサポートしています。UTF32 プロパティを通じても使用できます。

その他のエンコーディングを取得するには、アプリケーションで GetEncoding メソッドを使用します。すべてのエンコーディングの一覧を取得するには、GetEncodings メソッドを使用する必要があります。

次の表は、サポートされているエンコーディングと、それに関連付けられたコード ページの一覧を示しています。最後の列にアスタリスク (*) が付いているコード ページは、基になるプラットフォームに関係なく、.NET Framework でネイティブにサポートされています。

コード ページ

名前

表示名

37

IBM037

IBM EBCDIC (US - カナダ)

437

IBM437

OEM アメリカ合衆国

500

IBM500

IBM EBCDIC (インターナショナル)

708

ASMO-708

アラビア語 (ASMO 708)

720

DOS-720

アラビア語 (DOS)

737

ibm737

ギリシャ語 (DOS)

775

ibm775

バルト言語 (DOS)

850

ibm850

西ヨーロッパ言語 (DOS)

852

ibm852

中央ヨーロッパ言語 (DOS)

855

IBM855

OEM キリル

857

ibm857

トルコ語 (DOS)

858

IBM00858

OEM マルチリンガル ラテン I

860

IBM860

ポルトガル語 (DOS)

861

ibm861

アイスランド語 (DOS)

862

DOS-862

ヘブライ語 (DOS)

863

IBM863

フランス語 (カナダ) (DOS)

864

IBM864

アラビア語 (864)

865

IBM865

北欧 (DOS)

866

cp866

キリル言語 (DOS)

869

ibm869

ギリシャ語, Modern (DOS)

870

IBM870

IBM EBCDIC (多国語ラテン 2)

874

windows-874

タイ語 (Windows)

875

cp875

IBM EBCDIC (ギリシャ語 Modern)

932

shift_jis

日本語 (シフト JIS)

936

gb2312

簡体字中国語 (GB2312)

*

949

ks_c_5601-1987

韓国語

950

big5

繁体字中国語 (Big5)

1026

IBM1026

IBM EBCDIC (トルコ語ラテン 5)

1047

IBM01047

IBM ラテン-1

1140

IBM01140

IBM EBCDIC (US - カナダ - ヨーロッパ)

1141

IBM01141

IBM EBCDIC (ドイツ - ヨーロッパ)

1142

IBM01142

IBM EBCDIC (デンマーク - ノルウェー - ヨーロッパ)

1143

IBM01143

IBM EBCDIC (フィンランド - スウェーデン - ヨーロッパ)

1144

IBM01144

IBM EBCDIC (イタリア - ヨーロッパ)

1145

IBM01145

IBM EBCDIC (スペイン - ヨーロッパ)

1146

IBM01146

IBM EBCDIC (UK - ヨーロッパ)

1147

IBM01147

IBM EBCDIC (フランス - ヨーロッパ)

1148

IBM01148

IBM EBCDIC (インターナショナル - ヨーロッパ)

1149

IBM01149

IBM EBCDIC (アイスランド語 - ヨーロッパ)

1200

utf-16

Unicode

*

1201

unicodeFFFE

Unicode (ビッグ エンディアン)

*

1250

windows-1250

中央ヨーロッパ言語 (Windows)

1251

windows-1251

キリル言語 (Windows)

1252

Windows -1252

西ヨーロッパ言語 (Windows)

*

1253

windows-1253

ギリシャ語 (Windows)

1254

windows-1254

トルコ語 (Windows)

1255

windows-1255

ヘブライ語 (Windows)

1256

windows-1256

アラビア語 (Windows)

1257

windows-1257

バルト言語 (Windows)

1258

windows-1258

ベトナム語 (Windows)

1361

Johab

韓国語 (Johab)

10000

macintosh

西ヨーロッパ言語 (Mac)

10001

x-mac-japanese

日本語 (Mac)

10002

x-mac-chinesetrad

繁体字中国語 (Mac)

10003

x-mac-korean

韓国語 (Mac)

*

10004

x-mac-arabic

アラビア語 (Mac)

10005

x-mac-hebrew

ヘブライ語 (Mac)

10006

x-mac-greek

ギリシャ語 (Mac)

10007

x-mac-cyrillic

キリル言語 (Mac)

10008

x-mac-chinesesimp

簡体字中国語 (Mac)

*

10010

x-mac-romanian

ルーマニア語 (Mac)

10017

x-mac-ukrainian

ウクライナ語 (Mac)

10021

x-mac-thai

タイ語 (Mac)

10029

x-mac-ce

中央ヨーロッパ言語 (Mac)

10079

x-mac-icelandic

アイスランド語 (Mac)

10081

x-mac-turkish

トルコ語 (Mac)

10082

x-mac-croatian

クロアチア語 (Mac)

12000

utf-32

Unicode (UTF-32)

*

12001

utf-32BE

Unicode (UTF-32 ビッグ エンディアン)

*

20000

x-Chinese-CNS

繁体字中国語 (CNS)

20001

x-cp20001

TCA 台湾

20002

x-Chinese-Eten

繁体字中国語 (Eten)

20003

x-cp20003

IBM5550 台湾

20004

x-cp20004

TeleText 台湾

20005

x-cp20005

Wang 台湾

20105

x-IA5

西ヨーロッパ言語 (IA5)

20106

x-IA5-German

ドイツ語 (IA5)

20107

x-IA5-Swedish

スウェーデン語 (IA5)

20108

x-IA5-Norwegian

ノルウェー語 (IA5)

20127

us-ascii

US-ASCII

*

20261

x-cp20261

T.61

20269

x-cp20269

ISO -6937

20273

IBM273

IBM EBCDIC (ドイツ)

20277

IBM277

IBM EBCDIC (デンマーク - ノルウェー)

20278

IBM278

IBM EBCDIC (フィンランド - スウェーデン)

20280

IBM280

IBM EBCDIC (イタリア)

20284

IBM284

IBM EBCDIC (スペイン)

20285

IBM285

IBM EBCDIC (UK)

20290

IBM290

IBM EBCDIC (日本語カタカナ)

20297

IBM297

IBM EBCDIC (フランス)

20420

IBM420

IBM EBCDIC (アラビア語)

20423

IBM423

IBM EBCDIC (ギリシャ語)

20424

IBM424

IBM EBCDIC (ヘブライ語)

20833

x-EBCDIC-KoreanExtended

IBM EBCDIC (韓国語 Extended)

20838

IBM-Thai

IBM EBCDIC (タイ語)

20866

koi8-r

キリル言語 (KOI8-R)

20871

IBM871

IBM EBCDIC (アイスランド語)

20880

IBM880

IBM EBCDIC (キリル言語 - ロシア語)

20905

IBM905

IBM EBCDIC (トルコ語)

20924

IBM00924

IBM ラテン-1

20932

EUC-JP

日本語 (JIS 0208-1990 および 0212-1990)

20936

x-cp20936

簡体字中国語 (GB2312-80)

*

20949

x-cp20949

韓国語 Wansung

*

21025

cp1025

IBM EBCDIC (キリル言語 セルビア - ブルガリア)

21866

koi8-u

キリル言語 (KOI8-U)

28591

iso-8859-1

西ヨーロッパ言語 (ISO)

*

28592

iso-8859-2

中央ヨーロッパ言語 (ISO)

28593

iso-8859-3

ラテン 3 (ISO)

28594

iso-8859-4

バルト言語 (ISO)

28595

iso-8859-5

キリル言語 (ISO)

28596

iso-8859-6

アラビア語 (ISO)

28597

iso-8859-7

ギリシャ語 (ISO)

28598

iso-8859-8

ヘブライ語 (ISO-Visual)

*

28599

iso-8859-9

トルコ語 (ISO)

28603

iso-8859-13

エストニア語 (ISO)

28605

iso-8859-15

ラテン 9 (ISO)

29001

x-Europa

ヨーロッパ

38598

iso-8859-8-i

ヘブライ語 (ISO-Logical)

*

50220

iso-2022-jp

日本語 (JIS)

*

50221

csISO2022JP

日本語 (JIS 1 バイト カタカナ可)

*

50222

iso-2022-jp

日本語 (JIS 1 バイト カタカナ可 - SO/SI)

*

50225

iso-2022-kr

韓国語 (ISO)

*

50227

x-cp50227

簡体字中国語 (ISO-2022)

*

51932

euc-jp

日本語 (EUC)

*

51936

EUC-CN

簡体字中国語 (EUC)

*

51949

euc-kr

韓国語 (EUC)

*

52936

hz-gb-2312

簡体字中国語 (HZ)

*

54936

GB18030

簡体字中国語 (GB18030)

*

57002

x-iscii-de

ISCII デバナガリ文字

*

57003

x-iscii-be

ISCII ベンガル語

*

57004

x-iscii-ta

ISCII タミール語

*

57005

x-iscii-te

ISCII テルグ語

*

57006

x-iscii-as

ISCII アッサム語

*

57007

x-iscii-or

ISCII オリヤー語

*

57008

x-iscii-ka

ISCII カナラ語

*

57009

x-iscii-ma

ISCII マラヤラム語

*

57010

x-iscii-gu

ISCII グジャラート語

*

57011

x-iscii-pa

ISCII パンジャブ語

*

65000

utf-7

Unicode (UTF-7)

*

65001

utf-8

Unicode (UTF-8)

*

GetByteCount メソッドは、Unicode 文字のセットをエンコードした結果得られるバイト数を確認します。実際のエンコードは、GetBytes メソッドによって実行されます。

同様に、GetCharCount メソッドは、バイト シーケンスをデコードした結果得られる文字数を確認します。実際のデコードは GetChars メソッドによって実行されます。

変換するデータが一連のブロックの形でのみ得られる場合 (ストリームから読み込むデータなど)、または、データ容量が大きいために小さなブロックに分割する必要がある場合は、派生クラスの GetDecoder メソッドで得られる Decoder、または GetEncoder メソッドで得られる Encoder を使用します。

UTF-16 エンコーダおよび UTF-32 エンコーダでは、最上位バイトが先頭に配置されるビッグ エンディアン バイト順、または最下位バイトが先頭に配置されるリトル エンディアン バイト順が使用されます。たとえば、アルファベットの大文字 A (U+0041) は次のように 16 進数でシリアル化されます。

  • UTF-16 ビッグ エンディアン バイト順 : 00 41

  • UTF-16 リトル エンディアン バイト順 : 41 00

  • UTF-32 ビッグ エンディアン バイト順 : 00 00 00 41

  • UTF-32 リトル エンディアン バイト順 : 41 00 00 00

Encoding は、オプションでプリアンブルを提供します。プリアンブルは、エンコーディング プロセスで得られたバイト シーケンスの先頭に付加できるバイトの配列です。プリアンブルにバイト順マーク (Unicode では、コード ポイント U+FEFF) が含まれる場合、デコーダはバイト順および変換形式 (UTF) を判断できます。Unicode バイト順マークは、次のように 16 進数でシリアル化されます。

  • UTF-8 : EF BB BF

  • UTF-16 ビッグ エンディアン バイト順 : FE FF

  • UTF-16 リトル エンディアン バイト順 : FF FE

  • UTF-32 ビッグ エンディアン バイト順 : 00 00 FE FF

  • UTF-32 リトル エンディアン バイト順 : FF FE 00 00

通常、ネイティブなバイト順で Unicode 文字を格納した方が効率的です。たとえば、Intel のコンピュータなど、リトル エンディアンのプラットフォームでは、リトル エンディアンのバイト順を使用した方が効率的です。

GetPreamble メソッドは、バイト順マークを格納できるバイト配列を返します。このバイト配列がエンコード済みストリームの前に付加されていると、使用されているエンコーディング形式をデコーダが識別するのに役立ちます。

バイト順とバイト順マークの詳細については、www.unicode.org の「Unicode Standard」を参照してください。

次のコード例は、あるエンコーディングから別のエンコーディングへ文字列を変換しています。

Visual Basic
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 namespace System;
using namespace System::Text;
int 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->Item[].
   array<Byte>^unicodeBytes = unicode->GetBytes( unicodeString );
   
   // Perform the conversion from one encoding to the other.
   array<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.
   array<Char>^asciiChars = gcnew array<Char>(ascii->GetCharCount( asciiBytes, 0, asciiBytes->Length ));
   ascii->GetChars( asciiBytes, 0, asciiBytes->Length, asciiChars, 0 );
   String^ asciiString = gcnew String( asciiChars );
   
   // Display the strings created before and after the conversion.
   Console::WriteLine( "Original String*: {0}", unicodeString );
   Console::WriteLine( "Ascii converted String*: {0}", asciiString );
}
J#
package ConvertExample; 

import System.*;
import System.Text.*;

class ConvertExampleClass
{
    public static void main(String[] args)
    {
        String unicodeString = 
                "This string contains the unicode character Pi(\u03a0)";

        // Create two different encodings.
        Encoding ascii = Encoding.get_ASCII();
        Encoding unicode = Encoding.get_Unicode();

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

        // Perform the conversion from one encoding to the other.
        ubyte 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);
    } //main
} //ConvertExampleClass 
System.Object
  System.Text.Encoding
     派生クラス
この型の public static (Visual Basicでは共有) メンバはすべて,スレッド セーフです。インスタンス メンバの場合は,スレッド セーフであるとは限りません。

Windows 98,Windows Server 2000 SP4,Windows CE,Windows Millennium Edition,Windows Mobile for Pocket PC,Windows Mobile for Smartphone,Windows Server 2003,Windows XP Media Center Edition,Windows XP Professional x64 Edition,Windows XP SP2,Windows XP Starter Edition

Microsoft .NET Framework 3.0 は Windows Vista,Microsoft Windows XP SP2,および Windows Server 2003 SP1 でサポートされています。

.NET Framework

サポート対象 : 3.0,2.0,1.1,1.0

.NET Compact Framework

サポート対象 : 2.0,1.0

XNA Framework

サポート対象 : 1.0
コミュニティ コンテンツ   コミュニティ コンテンツとは
新しいコンテンツの追加 RSS  注釈
Processing
© 2010 Microsoft Corporation. All rights reserved. 使用条件 | 商標 | プライバシー
Page view tracker