Hi,
I see some suggestions for how to convert a QString to a char* (or const char*), but when I try them I get bad data.
For instance, I try this:
QString doh
= ("Blah, blah, blah");
cout << "The String " << qPrintable(doh) << endl;
// Convert to char*
const char *message = doh.toLocal8Bit ().constData (); //<--- DOES NOT WORK!!
cout << "The Message is " << message << endl
QString doh = ("Blah, blah, blah");
cout << "The String " << qPrintable(doh) << endl;
// Convert to char*
const char *message = doh.toLocal8Bit ().constData (); //<--- DOES NOT WORK!!
cout << "The Message is " << message << endl
To copy to clipboard, switch view to plain text mode
;
and the output looks like as follows:
The String Blah, blah, blah
The Message is ▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌
Then, if I try this the output is the same:
QString doh
= ("Blah, blah, blah");
cout << "The String " << qPrintable(doh) << endl;
// Convert to char*
const char *message = qPrintable(doh); //<--- DOES NOT WORK!!
cout << "The Message is " << message << endl
QString doh = ("Blah, blah, blah");
cout << "The String " << qPrintable(doh) << endl;
// Convert to char*
const char *message = qPrintable(doh); //<--- DOES NOT WORK!!
cout << "The Message is " << message << endl
To copy to clipboard, switch view to plain text mode
;
The only way I get legit output is doing the following:
QString doh
= ("Blah, blah, blah");
cout << "The String " << qPrintable(doh) << endl;
// Convert to char*
QByteArray encodedString
= codec
->fromUnicode
(doh
);
const char *message = encodedString.constData ();
cout << "The Message is " << message << endl;
QString doh = ("Blah, blah, blah");
cout << "The String " << qPrintable(doh) << endl;
// Convert to char*
QTextCodec *codec = QTextCodec::codecForName("UTF-8");
QByteArray encodedString = codec->fromUnicode(doh);
const char *message = encodedString.constData ();
cout << "The Message is " << message << endl;
To copy to clipboard, switch view to plain text mode
The output is then;
The String Blah, blah, blah
The Message is Blah, blah, blah
This can't be the cleanest way to accomplish getting the proper character string into my char*, can it?
Thanks,
Derrick
Bookmarks