Qt and unicode escape string

2019-04-10 09:27发布

I'm getting from server data using signal and slot. Here is slot part:

QString text(this->reply->readAll());

Problem is, that in text variable will be unicode escape, for example:

\u043d\u0435 \u043f\u0430\u0440\u044c\u0441\u044f ;-)

Is there any way to convert this?

3条回答
放荡不羁爱自由
2楼-- · 2019-04-10 09:46

I think this is what you needed:

(Find occurrences of \uCCCC using regex and replace them by the QChar with unicode number CCCC in base 16)

QRegExp rx("(\\\\u[0-9a-fA-F]{4})");
int pos = 0;
while ((pos = rx.indexIn(str, pos)) != -1) {
    str.replace(pos++, 6, QChar(rx.cap(1).right(4).toUShort(0, 16)));
}
查看更多
神经病院院长
3楼-- · 2019-04-10 10:01

Did you try:

QString text = QString::fromUtf8(this->reply->readAll());

http://doc.qt.io/qt-5/qstring.html#fromUtf8

Assuming it's Utf8, otherwise use fromUtf16

查看更多
在下西门庆
4楼-- · 2019-04-10 10:10

How about this one??

QString text = reply->readAll().replace("\","\\");

By using the above snippet, you can replace the single slash to double slash, so that the single slash can be obtained as such. Hope it works.

查看更多
登录 后发表回答