Princekin
Princekin

Reputation: 764

how get correct Chinese when use rapidjson on macos develop qt app?

I developed a cross-platform qt app on macos and windows,I have a file named 'proj.json', it generated by rapidjson in my app on windows, and on windows its encoding-format is GB2312, now I use rapidjson in my app on mac to analysis proj.json, in the proj.json file there have a string '.\Templates\2D贴纸\2D贴纸.fuproj', in my qt app on mac,I use this code to get the string '.\Templates\2D贴纸\2D贴纸.fuproj'

string pjson = fullfile(folder, "/proj.json");
    if (isexist(pjson))
    {
        rapidjson::Document doc;
        bool isReadJsonSuccess;
        doc = loadJson(pjson,&isReadJsonSuccess);
        if (isReadJsonSuccess){
            rapidjson::Value &ps = doc["Projects"];
            for (int i = 0; i < ps.Size(); i++) {
                rapidjson::Value &item = ps[i];
                string str1 = item["path"].GetString();
                                cout << "str1----" << str1 << endl;


and it logout 'str1----.\Templates\2D\314\371ֽ\2D\314\371ֽ.fuproj',I don't the encoding-format of string '.\Templates\2D\314\371ֽ\2D\314\371ֽ.fuproj', and my qt app on mac runtime encoding-format is 'UTF-8',it seems GB2312 convert to UTF-8 cause that. how let the GB2312 string '.\Templates\2D贴纸\2D贴纸.fuproj' shows right in qt app on macos, thanks a lot!

Upvotes: 0

Views: 247

Answers (1)

Jian Zhong
Jian Zhong

Reputation: 501

How about converting it by following method

#include <QTextCodec>

inline QString GBK2UTF8(const QString &inStr)
{
    QTextCodec *gbk = QTextCodec::codecForName("GB18030");
    QTextCodec *utf8 = QTextCodec::codecForName("UTF-8");

    QString g2u = gbk->toUnicode(gbk->fromUnicode(inStr));            // gbk  convert utf8
    return g2u;
}

inline QString UTF82GBK(const QString &inStr)
{
    QTextCodec *gbk = QTextCodec::codecForName("GB18030");
    QTextCodec *utf8 = QTextCodec::codecForName("UTF-8");

    QString utf2gbk = gbk->toUnicode(inStr.toLocal8Bit());
    return utf2gbk;
}

// Convert stirng to QString
std::string str = "Hello world";
QString qstr = QString::fromStdString(str);

I didn't test it, hode that can help.

Upvotes: 1

Related Questions