我有一个以 ANSI 格式编码的文件(在 Notepad++ 中显示为 ANSI 编码),它还显示特殊字符(摄氏度、磅等),在阅读时我想将所有字符转换为 unicode。
如何在 C/C++ 或 Qt 中将 ANSI 转换为 Unicode?
我的 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;
}
从 和 的文档拼凑而成QFile
,QTextStream
两者都包含读写文件的示例。默认为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).
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.