I know there is plenty of information about converting QString
to char*
, but I still need some clarification in this question.
Qt provides QTextCodec
s to convert QString
(which internally stores characters in unicode) to QByteArray
, allowing me to retrieve char*
which represents the string in some non-unicode encoding. But what should I do when I want to get a unicode QByteArray
?
QTextCodec* codec = QTextCodec::codecForName("UTF-8");
QString qstr = codec->toUnicode("Юникод");
std::string stdstr(reinterpret_cast<const char*>(qstr.constData()), qstr.size() * 2 ); // * 2 since unicode character is twice longer than char
qDebug() << QString(reinterpret_cast<const QChar*>(stdstr.c_str()), stdstr.size() / 2); // same
The above code prints "Юникод" as I've expected. But I'd like to know if that is the right way to get to the unicode char*
of the QString
. In particular, reinterpret_cast
s and size arithmetics in this technique looks pretty ugly.