qDebug() 的错误输出(UTF - 8) [英] Wrong output of qDebug() (UTF - 8)
问题描述
我正在尝试存储一个带有特殊字符的字符串::
I'm trying to store a string with special chars::
qDebug() << "ÑABCgÓ";
输出:(在这里我什至无法输入正确的输出,在 Ã & Ã 之后缺少一些垃圾)ÃABCgÃ
Outputs: (here i can't even type the correct output some garbage is missing after à & Ã) ÃABCgÃ
我怀疑某些 UTF-8/Latin1/ASCII,但找不到输出到控制台/文件的设置.我在代码中写的内容:ÑABCgÓ".
I suspect some UTF-8 / Latin1 / ASCII, but can't find the setting to output to console / file. What i have written in my code : "ÑABCgÓ".
(Qt:4.8.5/Ubunto 12.04/C++98)
(Qt:4.8.5 / Ubunto 12.04 / C++98)
推荐答案
您可以使用 QString QString::fromUtf8(const char * str, int size = -1) [static]
作为示例代码如下所示.这是QString存在的主要原因之一.
You could use the QString QString::fromUtf8(const char * str, int size = -1) [static]
as the sample code presents that below. This is one of the main reasons why QString exists.
查看文档了解详情:
http://qt-project.org/doc/qt-5.1/qtcore/qstring.html#fromUtf8
#include <QString>
#include <QDebug>
int main()
{
qDebug() << QString::fromUtf8("ÑABCgÓ");
return 0;
}
建筑(根据您的场景定制)
g++ -fPIC -I/usr/include/qt -I/usr/include/qt/QtCore -lQt5Core main1000.cpp && ./a.out
输出
"ÑABCgÓ"
话虽如此,根据您的语言环境,只需 qDebug() <<"ÑABCgÓ";
可以像这里一样工作,但建议通过明确询问 UTF-8 处理来确保.
That being said, depending on your locale, simply qDebug() << "ÑABCgÓ";
could work as well like in here, but it is recommended to make sure by explicitly asking the UTF-8 handling.
这篇关于qDebug() 的错误输出(UTF - 8)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!