1

我有一个以 ANSI 格式编码的文件(在 Notepad++ 中显示为 ANSI 编码),它还显示特殊字符(摄氏度、磅等),在阅读时我想将所有字符转换为 unicode。

如何在 C/C++ 或 Qt 中将 ANSI 转换为 Unicode?

4

3 回答 3

1

我的 Qt 仍然很生锈,但大致如下:

QFile inFile("foo.txt");
if (!inFile.open(QIODevice::ReadOnly | QIODevice::Text))
    return;

QFile outFile("foo.out.txt");
if (!outFile.open(QIODevice::WriteOnly | QIODevice::Truncate))
    return;

QTextStream in(&inFile);
QTextStream out(&outFile);
out.setCodec("UTF-8");

while (!in.atEnd()) {
    QString line = in.readLine();
    out << line;
}

从 和 的文档拼凑而成QFileQTextStream两者都包含读写文件的示例。默认为QTextStream使用 legacy 编码,因此我们只需要在 output 上设置显式编码QTextStream

如果文件不是太大,你也可以使用

out << in.readAll();

instead of the loop over the lines. The loop especially might add a trailing line break to the output file (although the docs aren't very clear on that).

于 2012-04-26T05:49:36.283 回答
0

希望这可以帮助。

http://geekswithblogs.net/dastblog/archive/2006/11/24/98746.aspx

于 2012-04-26T05:42:10.120 回答
0

Just read it with QTextStream. It will apply QTextCodec::codecForLocale, which uses the default ("ANSI") translation of 8 bits characters to Unicode.

Note that this won't work if you've copied an ANSI text file to Mac or Linux, as they don't have the notion of ANSI. For them, the ANSI text file will be ASCII-like so you should first convert to Unicode (UTF-8) and then copy.

于 2012-04-26T07:48:10.597 回答