There is no such beast as a "utf8 QString". QString is a collection of QChar, essentially 16-bit Unicode basic multilingual plane code points that are trivially accessible using QString::at() or other methods. The file or stream you are reading from may be UTF-8 encoded and decoded by QTextStream.
#include <QCoreApplication>
#include <QFile>
#include <QTextStream>
#include <QDebug>
int main(int argc, char **argv)
{
in.setCodec("UTF-8");
qDebug() << line;
for(int i = 0; i < line.size(); ++i) {
const ushort code = line.at(i).unicode();
if (code < 0x0080)
result += line.at(i);
else
}
qDebug() << result;
}
return 0;
}
#include <QCoreApplication>
#include <QFile>
#include <QTextStream>
#include <QDebug>
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
QFile file("test.txt");
if (file.open(QIODevice::ReadOnly)) {
QTextStream in(&file);
in.setCodec("UTF-8");
QString line = in.readLine();
qDebug() << line;
QString result;
for(int i = 0; i < line.size(); ++i) {
const ushort code = line.at(i).unicode();
if (code < 0x0080)
result += line.at(i);
else
result += QString("\\u%1").arg(code, 4, 16, QChar('0'));
}
qDebug() << result;
}
return 0;
}
To copy to clipboard, switch view to plain text mode
Output:
"test €1234"
"test \u20ac1234"
"test €1234"
"test \u20ac1234"
To copy to clipboard, switch view to plain text mode
Bookmarks