mikan's technical note

仕事&趣味で実験した小技の備忘録です(Linux,windows,DOS等)

MENU

【C#】ファイルの文字エンコード変換

【変換前】
f:id:myerss555:20170205182801p:plain

【変換後】
f:id:myerss555:20170205182809p:plain

//
// 【ファイルの文字エンコード変換】※Windows7で動作確認済み
//     「Shift-JIS -> UTF-8」,「UTF8 -> Shift-JIS」
// (c) 2017 mikan
// ※使用にあたっては利用者の自己責任でお願いします。
//
using System;
using System.IO;
using System.Text;

namespace EncodeChg
{
    class Program
    {
        static void Main(string[] args)
        {
            SjisToUtf8(@"d:\debug\in_sjis.txt", @"d:\debug\out_utf8.txt");
            Utf8ToSjis(@"d:\debug\in_utf8.txt", @"d:\debug\out_sjis.txt");
        }

        // Shift-JIS -> UTF-8 変換(BOMなし)
        static int SjisToUtf8(string fname1, string fname2)
        {
            // ※既存ファイルへの上書きチェック等は別途行ってください

            // ファイルをbyte形で全て読み込み
            FileStream fs1 = new FileStream(fname1, FileMode.Open);
            byte[] data = new byte[fs1.Length];
            fs1.Read(data, 0, data.Length);
            fs1.Close();

            // Shift-JIS -> UTF-8 変換(byte形)
            Encoding sjisEnc = Encoding.GetEncoding("Shift_JIS");
            string sjisstr = sjisEnc.GetString(data);
            byte[] bytesData = System.Text.Encoding.UTF8.GetBytes(sjisstr);

            // string型に変換したい場合はこんな感じに
            // Encoding utf8Enc = Encoding.GetEncoding("UTF-8");
            // string utf = utf8Enc.GetString(bytesData);
            // Console.WriteLine(utf);

            // 出力ファイルオープン(バイナリ形式)
            FileStream fs2 = new FileStream(fname2, FileMode.Create);
            // 書き込み設定(デフォルトはUTF-8)
            BinaryWriter bw = new BinaryWriter(fs2);

            // 出力ファイルへ全て書き込み
            bw.Write(bytesData);
            bw.Close();
            fs2.Close();

            return 0;
        }

        // UTF8 -> Shift-JIS 変換
        static int Utf8ToSjis(string fname1, string fname2)
        {
            // ※既存ファイルへの上書きチェック等は別途行ってください

            // ファイルをbyte形で全て読み込み
            FileStream fs1 = new FileStream(fname1, FileMode.Open);
            byte[] data = new byte[fs1.Length];
            fs1.Read(data, 0, data.Length);
            fs1.Close();

            // UTF-8 -> Shift-JIS 変換(byte形)
            Encoding utf8Enc = Encoding.GetEncoding("UTF-8");
            string utf8str = utf8Enc.GetString(data);
            byte[] bytesData =
                System.Text.Encoding.GetEncoding("Shift_JIS").GetBytes(utf8str);

            // string型に変換したい場合はこんな感じに
            // Encoding sjisEnc = Encoding.GetEncoding("Shift-JIS");
            // string sjis = sjisEnc.GetString(bytesData);
            // Console.WriteLine(sjis);

            // 出力ファイルオープン(バイナリ形式)
            FileStream fs2 = new FileStream(fname2, FileMode.Create);
            // 書き込み設定(Shift-JIS)
            BinaryWriter bw = new 
                BinaryWriter(fs2, System.Text.Encoding.GetEncoding("Shift_JIS"));

            // 出力ファイルへ全て書き込み
            bw.Write(bytesData);
            bw.Close();
            fs2.Close();

            return 0;
        }
    }
}