将 GB2312 转换为 UTF-8 [英] Convert GB2312 to UTF-8

查看:33
本文介绍了将 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

这篇关于将 GB2312 转换为 UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆