2

SQL Server Management Studio 2005 を使用して CSV ファイルをエクスポートしようとしています。[保存] の横にあるドロップ ダウン ボタンをクリックしてエンコード UTF を選択しようとしましたが、それでも UCS-2 リトル エンディアン エンコードとして保存されます。

Notepad ++で開いてUTF-8として保存することなく、UTF-8エンコーディングにすることは可能ですか? 余分なステップとすべて。

4

2 に答える 2

1

MS SQL Server は UCS-2 リトル エンディアンまたは ASII エンコーディングとしてのみ出力するため、bcp を使用して小さな C# プログラムを呼び出し、UTF-8 に変換することができます。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UCS2toUTF8
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.WriteLine("exampe: UCS2toUTF8 [filepath]");
                return;
            }

            var filename = args[0];

            var filestream = File.OpenRead(filename);

            var BOM = new byte[3];

            filestream.Read(BOM, 0, BOM.Length);

            if (BOM[0] != (byte)255 && BOM[1] != (byte)254 ) //0xff 0xfe 0x48
            {
                Console.WriteLine("This isn't UCS-2LE");
                return;
            }else if (BOM[0] == 0xEF && BOM[1] == 0xBB && BOM[2] == 0xBF)
            {
                Console.WriteLine("This is UTF-8");
                return;
            }

            filestream.Close();

            byte[] content = File.ReadAllBytes(filename);

            byte[] utf8Bytes = System.Text.Encoding.Convert(System.Text.Encoding.Unicode, System.Text.Encoding.UTF8, content);

            byte[] newArray = new byte[utf8Bytes.Length - 3];

            Array.Copy(utf8Bytes, 3, newArray, 0, newArray.Length);

            File.WriteAllBytes(filename, newArray);
        }
    }
}

BOM 付きの UTF-8 が必要な場合は、いくつかの変更を行う必要があることに注意してください。

于 2015-07-10T08:53:25.097 に答える