我有一些简单的代码可以反转QString。
const QString reverse_qstring(const QString& str_in)
{
QString out;
Q_FOREACH(const QChar c, str_in) {
out.push_front(c);
}
return out;
}当我从命令行输入包含非ASCII字符的文本时,结果与预期一致:
"¿como estás?" : "?sátse omoc¿"但是,当我进行以下单元测试时(使用QTestLib):
QCOMPARE(reverse_qstring(QString("¿como estás?")), QString("?sátse omoc¿"));我得到了:
FAIL! : program::test_qstring() Compared values are not the same
Actual (reverse_qstring(QString("??como est??s?"))): ?s??tse omoc??
Expected (QString("?s??tse omoc??")): ?s??tse omoc??有什么想法吗?
发布于 2013-03-03 09:36:18
我想你可以设置Utf8的编解码器:http://doc.qt.io/qt-4.8/qtextcodec.html#setCodecForCStrings
或者,您可以使用以下代码:QString::fromUtf8("?sátse omoc¿")
https://stackoverflow.com/questions/15177039
复制相似问题