-3

I think it's an encoding problem. I try to convert from UTF-8 to QT's string type, but it still doesn't work. How can I solve this problem?

#include <QtCore/QCoreApplication>

#include <iostream>
#include <string>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QString q_str = "ເລີ່ມ";
    QString q_str1 =QString::fromUtf8("ເລີ່ມ");
    
    std::string str = "ຈົບ";

    std::string begin = q_str.toStdString().data();
    std::string begin1 = q_str.toLocal8Bit().data();
    std::cout << begin << " " << begin1 << std::endl;

    return a.exec();
}

result

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
yong57
  • 1
  • 2
  • 2
    Are you on Windows? – prapin Aug 31 '23 at 12:20
  • 1
    The first problem to solve is to know what encoding is being used for your string literals. You seem to think that it is UTF-8 but I doubt that is true. – john Aug 31 '23 at 12:41
  • It is windows. I checked that the active code page of the windows system is 936 (GBK). The IDE I use is vs2015. The encoding in the advanced saving options is: Unicode (UTF-8 with signature) - code page 65001. The character set in the project-properties page is: Use Unicode character set – yong57 Sep 01 '23 at 01:52
  • 1
    What version of C++ are you using? If C++11 or later, consider using the `u8` prefix on your string literals, eg: `QString::fromUtf8(u8"ເລີ່ມ");` Also, `QString::toStdString()` returns a `std::string`, so you don't need to call `data()` on it when assigning the result to another `std::string` – Remy Lebeau Sep 01 '23 at 03:10
  • I tried your suggestion, adding ’u8‘ is effective, but when converted to string it shows garbled code, regardless of whether there is ‘data()’ or not it is garbled@ Remy Lebeau – yong57 Sep 01 '23 at 04:17
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 01 '23 at 07:01

0 Answers0