I am mixed up with std::string, std::wstring, QString, anscii, utf8, unicode and charset
-
Ops...I didn't notice it!
-
[quote author="fluca1978" date="1318331294"]Ops...I didn't notice it![/quote]
No problem :-)
Qt framework is big enough for someone to know everything ... ( at least I think so :-) ) -
My new implement is like this:
@
#pragma once#include <string>
#include <QString>class CStringConverter {
public:
static std::wstring Ansi2Unicode(std::string szAnsi) {
QString s = QString::fromAscii(szAnsi.c_str());
return s.toStdWString();
}
static std::string Unicode2Ansi(std::wstring szUnicode) {
QString s = QString::fromStdWString(szUnicode);
return s.toAscii();
}static std::wstring Utf82Unicode(std::string szUtf8) { QString s = QString::fromUtf8(szUtf8.c_str()); return s.toStdWString(); } static std::string Unicode2Utf8(std::wstring szUnicode) { QString s = QString::fromStdWString(szUnicode); return s.toUtf8(); } static std::string Ansi2Utf8(std::string szAnsi) { return Unicode2Utf8(Ansi2Unicode(szAnsi)); } static std::string Utf82Ansi(std::string szUtf8) { return Unicode2Ansi(Utf82Unicode(szUtf8)); }
};
@
But there are still problem.
1).it can be compiled using qt under windows, but can not be compiled under mac?
@
static std::string Unicode2Ansi(std::wstring szUnicode) {
QString s = QString::fromStdWString(szUnicode);
//in this statement, the error is:
//converstion from 'QByteArray' to non-scalar type 'std::string' requested
return s.toAscii();
}
@ -
BTW,I try to create a "Qt Console Application" in Mac
@
#untitiled2.pro
QT += coreQT -= gui
DEFINES += UNICODE
TARGET = untitled2
CONFIG += console
CONFIG -= app_bundleTEMPLATE = app
SOURCES += main.cpp
@
@
//main.cpp
#include <QtCore/QCoreApplication>#include<string>
#include<QString>int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);std::string stdStr = "abc123你我他_stdStr"; std::wstring stdWStr = L"abc123你我他_stdWStr"; QString qStr="abc123你我他_qStr"; printf(stdStr.c_str()); printf("\n"); wprintf(stdWStr.c_str()); printf("\n"); wprintf(qStr.toStdWString().c_str()); printf("\n"); return a.exec();
}
@when I run in debug mode, I add a breakpoints before
@
return a.exec();
@
and open "Locals and Expressions" window,
but in that window, there are not any string value.
the table in "Locals and Expressions" window is:
@
Name Value TypeqStr QString
stdStr string
stdWStr wstring
@the "value" field are null.
I may want to view at the "value" field: "abc123你我他_stdStr","abc123你我他_stdWStr","abc123你我他_qStr", for the std::string,std::wstring,QString variable value.Is any way for me to watch the std::string, std::wstring, QString variable value, when I debug?
And when I watch X11 console,it print as:
abc123ä½ æä»_stdStr
abc123_stdWStr
abc123ä½ æä»_qStrmaybe, it look like don't support chinese or japanese language.
How can I make X11 console to print exactly?
as:
abc123你我他_stdStr
abc123你我他_stdWStr
abc123你我他_qStrbut this program run under window.
the table in "Locals and Expressions" window is:
@
Name Value TypeqStr "abc123ÄãÎÒËû_qStr" QString
stdStr 0x3f9da0 "abc123???" string
stdWStr class std::basic_string<> wstring
@
And the console print as:
abc123你我他stdStr
abc123
abc123你我他_qStr -
[quote author="szuzsq" date="1318393533"]
1).it can be compiled using qt under windows, but can not be compiled under mac?
@
static std::string Unicode2Ansi(std::wstring szUnicode) {
QString s = QString::fromStdWString(szUnicode);
//in this statement, the error is:
//converstion from 'QByteArray' to non-scalar type 'std::string' requested
return s.toAscii();
}
@[/quote]Your "Unicode2Ansi" function involve calling "QByteArray::operator const char * ()":http://doc.qt.nokia.com/latest/qbytearray.html#operator-const-char--2a Maybe on Mac QT_NO_CAST_FROM_BYTEARRAY is implicity defined.
Why you do not call directly "QString::toStdString":http://doc.qt.nokia.com/latest/qstring.html#toStdString ? -
Because of I using 2 different sdk, and someone using char*, and someone using wchat_t*.
so I must convert between them.In the last demo, I use std::string, QString, std::wstring(convert to std::string), to write a simple xml file.
@
#include "tinyxml.h"#include <string>
#include <QString>int main(int argc, char* argv[]) {
TiXmlDocument doc;
TiXmlElement elem("rene");//directly using std::string and QString std::string s = "_你_"; elem.SetAttribute("s", s.c_str()); QString qs = "_我_"; elem.SetAttribute("qs",qs.toStdString().c_str()); //convert std::wstring to std::string //using by QString for adapter std::wstring ws = L"_他_"; QString qs2 = QString::fromStdWString(ws); std::string s2 = qs2.toStdString(); elem.SetAttribute("ws", s2.c_str()); doc.InsertEndChild(elem); doc.SaveFile("0.xml");
}
@but the result xml file have some error:
@
<rene s="你" qs="我" ws="?" />
@and I look forward to the right xml file like:
@
<rene s="你" qs="我" ws="他" />
@ -
when I invoke
@
char* rs = setlocale(LC_CTYPE, "");
@in windows, the result is: "Chinese_People's Repulic of China 936";
but in mac, the result is :"C".Is there any wrong?
-
Well, then have a look at "convert a QString to char*":http://developer.qt.nokia.com/faq/answer/how_can_i_convert_a_qstring_to_char_and_vice_versa using locale settings.
-
[quote author="cincirin" date="1318412939"]Well, then have a look at "convert a QString to char*":http://developer.qt.nokia.com/faq/answer/how_can_i_convert_a_qstring_to_char_and_vice_versa using locale settings.[/quote]
thank you, but the wrong is also exist.
in fact, if I use QString::fromStdString, fromStdWString, toStdString, toStdWString to convert between std::string and std::wstring,
with latin or ascii word, it run ok, not wrong;
but with chinese or japanese word, it run wrong.... -
Note that your compiler may actually have trouble reading non-ascii characters from your file.
-
from/toStdString always converts the text. If you want to be safe, you MUST know, which char* convention you use. Codepage based? Utf-8?
I personally prefer utf-8, which is simple with QString:
@
QByteArray ba = string.toUtf8();
@The byte array has the correct length and a const char* conversion so you can put it to an std::string.