Nominate our 2022 Qt Champions!

How convert Qsting to const char * ?

  • how convert Qsting to const char * on that support unicode format?

  • Moderators

    QString string("blah blah");

    const char *raw = string.toUtf8().constData();

  • For UNICODE string I recommend the using of wchar_t* instead of char*. Or even QChar*. You can get it with data() method (note that size of QChar is 2 bytes like wchar_t in most of OS).

    But if you want raw bytes - you must receive QByteArray first with one of methods like toUtf8 or toLatin1.

  • Moderators

    Quoting Unicode 4.0:
    [quote]The width of wchar_t is compiler-specific and can be as small as 8 bits. Consequently, programs that need to be portable across any C or C++ compiler should not use wchar_t for storing Unicode text. The wchar_t type is intended for storing compiler-defined wide characters, which may be Unicode characters in some compilers.[/quote]

    In any case, just as QtRoS hinted, you should stick qith Qt stuff as much as possible, as it's guaranteed to be cross-platform.

Log in to reply