将GB2312转换为UTF-8

问题描述:

我有一个文本文件,该文件包含当前以GB2312(简体中文)编码的本地化语言字符串,但是我所有其他语言文件都使用UTF-8.我发现使用此文件非常困难,因为我的文本编辑器都无法正常使用它并不断损坏它.是否有任何工具可以将其转换为UTF-8,并且有任何不利之处?最好仅将其保留为GB2312并使用其他编辑器(如果可以的话,可以推荐一个)吗?

I have a text file that contains localized language strings that is currently encoded in GB2312 (simplified Chinese), but all of my other language files are in UTF-8. I am finding it very difficult to work with this file, as none of my text editors will work properly with it and keep corrupting it. Are there any tools to convert this to UTF-8, and are there any downsides to doing this? Would it be better to just keep it as GB2312 and use a different editor (if so, can you recommend one)?

更新:我正在使用Windows XP(英语安装).

Update: I'm using Windows XP (English install).

更新#2:我曾尝试使用Notepad ++和Notepad2编辑GB2312文件,但两者均无法读取文件并损坏它们.

Update #2: I've tried using Notepad++ and Notepad2 to edit the GB2312 files, but both are unable to read the files and corrupt them.

您可以尝试以下在线服务使用开源iconv实用程序.
您还可以在计算机上安装 Charco 的命令行版本.

You can try this online service that uses the Open Source iconv utility.
You can also install Charco, a command-line version of it on your machine.

对于GB2312,您可以使用CP936作为编码.

For GB2312, you can use CP936 as the encoding.

如果您是.Net开发人员,则可以制作一个可以执行此操作的小工具.
我也为此付出了很多努力,发现从编程的角度来看,解决问题实际上很简单.

If you are a .Net developer you can make a small tool that does just that.
I've struggled with this as well and found that it was actually simple to solve from a programmatic point of view.

您需要的是这样的东西(我对其进行了测试,并且可以正常工作):

All you need is something like this (I tested it and it works):

在C#中

static void Main(string[] args) {
    string infile = args[0];
    string outfile = args[1];

    using (StreamReader sr = new StreamReader(infile, Encoding.GetEncoding(936))) {
        using (StreamWriter sw = new StreamWriter(outfile, false, Encoding.UTF8)) {
            sw.Write(sr.ReadToEnd());
            sw.Close();
        }
        sr.Close();
    }
}

在VB.Net中

Private Shared Sub Main(ByVal args() As String)
    Dim infile As String = args(0)
    Dim outfile As String = args(1)
    Dim sr As StreamReader = New StreamReader(infile, Encoding.GetEncoding(936))
    Dim sw As StreamWriter = New StreamWriter(outfile, false, Encoding.UTF8)
    sw.Write(sr.ReadToEnd)
    sw.Close
    sr.Close
End Sub