文字列のコードページを変換する

注意:
この文書は以前「.NETでいきまっしょい!」で公開していたものですが、公開以降メンテナンスされていません。 今や古い情報となった内容が記載されている場合があるのでご注意ください。

 System.Text名前空間にあるEncodingクラスのConvert()メソッドを用いると、文字列のコードページを変換することができる。 次のサンプルは、Unicodeで表された文字列を各コードページに変換し、その内部表記(バイト配列)とバイト長を表示するものである。
VB.NET
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
Imports System
Imports System.Text

Module Module1

    Sub Main()

        Dim source(), encoded() As Byte

        ' 元のエンコーディングはUnicode
        Dim sourceEncoding As Encoding = Encoding.Unicode

        ' 変換するエンコーディング
        Dim encodings() As Encoding = New Encoding() { _
                                Encoding.GetEncoding("iso-2022-jp"), _
                                Encoding.GetEncoding("shift_jis"), _
                                Encoding.GetEncoding("euc-jp"), _
                                Encoding.GetEncoding("utf-8"), _
                                Encoding.GetEncoding("unicodefffe")}

        ' 文字列をバイト配列に変換
        source = sourceEncoding.GetBytes("ABCあいうアイウ漢字")

        ' 各エンコーディングで変換
        Dim enc As Encoding

        For Each enc In encodings

            ' エンコーディング名を表示
            Console.WriteLine("Encoding: {0}", enc.EncodingName)

            ' 指定されたエンコーディングで変換
            encoded = Encoding.Convert(sourceEncoding, enc, source)

            ' 変換されたバイト配列を文字列として表示する
            Console.WriteLine("{0}, 計{1}バイト", enc.GetString(encoded), encoded.Length)

            ' バイト配列をダンプする
            DumpByteArray(encoded)

            ' 単なる改行
            Console.WriteLine()

        Next

    End Sub

    Sub DumpByteArray(ByVal arr() As Byte)

        Dim pos As Integer = 0
        Dim len As Integer = arr.Length

        Do

            Dim i As Integer
            Dim c As Integer

            If len <= pos + &H10 Then

                c = len - pos

            Else

                c = &H10

            End If

            For i = 0 To c - 1

                Console.Write("{0:X2} ", arr(pos + i))

            Next

            Console.WriteLine()

            pos += &H10

        Loop Until len <= pos

    End Sub

End Module
C#
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
using System;
using System.Text;

namespace TextCodec
{
    class Class1
    {
        [STAThread]
        static void Main(string[] args)
        {
            byte[] source, encoded;

            // 元のエンコーディングはUnicode
            Encoding sourceEncoding = Encoding.Unicode;

            // 変換するエンコーディング
            Encoding[] encodings = new Encoding[] {
                                        Encoding.GetEncoding("iso-2022-jp"),   // JIS, 50222
                                        Encoding.GetEncoding("shift_jis"),     // シフトJIS, 932
                                        Encoding.GetEncoding("euc-jp"),        // 日本語EUC, 15932
                                        Encoding.GetEncoding("utf-8"),         // UTF-8, 65001
                                        Encoding.GetEncoding("unicodefffe") }; // Unicode(Big-Endian). 1201

            // 文字列をバイト配列に変換
            source = sourceEncoding.GetBytes( "ABCあいうアイウ漢字" );

            // 各エンコーディングで変換
            foreach ( Encoding encoding in encodings )
            {
                // エンコーディング名を表示
                Console.WriteLine( "Encoding: {0}", encoding.EncodingName );

                // 指定されたエンコーディングで変換
                encoded = Encoding.Convert( sourceEncoding, encoding, source );

                // 変換されたバイト配列を文字列として表示する
                Console.WriteLine( "{0}, 計{1}バイト", encoding.GetString( encoded ), encoded.Length );

                // バイト配列をダンプする
                DumpByteArray( encoded );

                // 単なる改行
                Console.WriteLine();
            }
        }

        // バイト配列をダンプする
        static void DumpByteArray( byte[] arr )
        {
            int pos = 0;
            int len = arr.Length;

            do
            {
                int i;
                int c;

                if ( len <= pos + 0x10 ) 
                {
                    c = len - pos;
                }
                else
                {
                    c = 0x10;
                }

                for ( i = 0; i <= c - 1; i += 1 )
                {
                    Console.Write( "{0:X2} ", arr[pos + i]);
                }

                Console.WriteLine();

                pos += 0x10;

            } while ( !( ( len <= pos ) ) );
        }
    }
}
出力結果
Encoding: 日本語 (JIS)
ABCあいうアイウ漢字, 計25バイト
41 42 43 1B 24 42 24 22 24 24 24 26 25 22 25 24
25 26 34 41 3B 7A 1B 28 42

Encoding: 日本語 (シフト JIS)
ABCあいうアイウ漢字, 計19バイト
41 42 43 82 A0 82 A2 82 A4 83 41 83 43 83 45 8A
BF 8E 9A

Encoding: 日本語 (EUC)
ABCあいうアイウ漢字, 計19バイト
41 42 43 A4 A2 A4 A4 A4 A6 A5 A2 A5 A4 A5 A6 B4
C1 BB FA

Encoding: Unicode (UTF-8)
ABCあいうアイウ漢字, 計27バイト
41 42 43 E3 81 82 E3 81 84 E3 81 86 E3 82 A2 E3
82 A4 E3 82 A6 E6 BC A2 E5 AD 97

Encoding: Unicode (Big-Endian)
ABCあいうアイウ漢字, 計22バイト
00 41 00 42 00 43 30 42 30 44 30 46 30 A2 30 A4
30 A6 6F 22 5B 57

Press any key to continue

 各コードページの番号とコードページ名を表にまとめると次のようになる。 Encoding.GetEncoding()メソッドにはコードページ番号を指定することもできる。
各コードページの番号と名前
コードページコードページ番号コードページ名
JIS50222iso-2022-jp
シフトJIS932shift_jis
日本語EUC15932euc-jp
Unicode1200utf-16
Unicode(Big-Endian)1201unicodefffe
UTF-765000utf-7
UTF-865001utf-8