简体   繁体   English

qDebug输出QString UTF-8非Ascii符号,如\\ uxxxx

[英]qDebug outputs QString UTF-8 non-Ascii symbols as like \uxxxx

I am trying to convert string (QString) in unicode to utf-8. 我正在尝试将Unicode中的字符串(QString)转换为utf-8。

qDebug prints string like this: qDebug打印这样的字符串:

"Fault code soap:Client: \П\о\л\ь\з\о\в\а\т\е\л\ь \с \и\д\е\н\т\и\ф\и\к\а\т\о\р\о\м \«16163341545811\» \н\е \н\а\й\д\е\н" “故障代码肥皂:客户端:\\ u041F \\ u043E \\ u043B \\ u044C \\ u0437 \\ u043E \\ u0432 \\ u0430 \\ u0442 \\ u0435 \\ u043B \\ u044C \\ u0441 \\ u0438 \\ u0434 \\ u0435 \\ u043D \\ u043D \\ u0442 \\ u0438 \\ u0444 \\ u0435 u043A \\ u0430 \\ u0442 \\ u043E \\ u0440 \\ u043E \\ u043C \\ u00AB16163341545811 \\ u00BB \\ u043D \\ u0435 \\ u043D \\ u0430 \\ u0439 \\ u0434 \\ u0435 \\ u043D”

I have tried using QTextCodec like this but it outputs same unreadable string: 我已经尝试过像这样使用QTextCodec但是它输出相同的不可读字符串:

QTextCodec *codec = QTextCodec::codecForName("UTF-8");
QString readableStr = QString::fromUtf8(codec->fromUnicode(str));

What am I doing wrong? 我究竟做错了什么?

EDIT: 编辑:

I wonder what is going on but it happens when qDebug prints QString ... 我不知道发生了什么,但是当qDebug打印QString时会发生...

The following code 以下代码

qDebug() << QString::fromUtf8("тест") << "тест" << QString::fromUtf8("тест").toUtf8().data();

prints out: 打印出:

"\т\е\с\т" тест тест “ \\ u0442 \\ u0435 \\ u0441 \\ u0442”тесттест

I assume that the variable str has type QString. 我假设变量str的类型为QString。 Your readableStr has the same contents as str. 您的readstr具有与str相同的内容。 UTF-8 is an encoding of Unicode strings that uses 8 bit characters, that can be stored in a QByteArray. UTF-8是使用8位字符的Unicode字符串的编码,可以存储在QByteArray中。 qDebug uses some special functions to display string in an console or debugging buffer to help you understand the contents of the string. qDebug使用一些特殊功能在控制台或调试缓冲区中显示字符串,以帮助您了解字符串的内容。 If you put a QString in any GUI element you will see the expected readable content. 如果将QString放在任何GUI元素中,您将看到预期的可读内容。

I don't know the exact thread on the Qt mailing list, but this behaviour was recently introduced, as qDebug is originally meant to debug an objects internal state. 我不知道Qt邮件列表上的确切线程,但是最近引入了此行为,因为qDebug最初旨在调试对象的内部状态。 Non ASCII characters are now put out like this, which most people seem to dislike, but the developer or maintainer responsible wants to keep it this way. 现在,大多数人似乎都不喜欢这样的非ASCII字符,但负责的开发人员或维护人员希望保持这种方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM