简体   繁体   中英

Convert QString with UTF-8 (hex) into QString

Basically, I have a string like "Sam\xe2\x80\x99s phone" and I want to convert that to be "Sam's phone" .

What is the easiest way to do that in Qt or C++?

I could loop through every character and look for \x , and convert all characters to hex values (except the two digits after every \x ), and then convert it to string but is there a better way?

EDIT:

void someFunction(int exitCode, QProcess::ExitStatus exitStatus){
    QProcess *someQProcess = reinterpret_cast<QProcess*>(sender());
    QString output = someQProcess->readAllStandardOutput();
    QStringList data = output.split("\n");
    parseScan(data);
}

void parseScan(QStringList data){
    QStringList nameList;
    for(int i = 2; i < data.size(); i++ ){
        QStringList dataLine  = data[i].split("\t");
        if(dataLine.size() == 5){
            QString name = dataLine[4];  // name is "Sam\xe2\x80\x99s phone"
        }
    }
}

Thanks

I think the problem comes from the way you read the standard output. Indeed, your standard output has a poor chance to be utf-8.
I tried to reproduce the same behavior with echo :

QProcess *p = new QProcess();
p->start("echo \"Sam\xe2\x80\x99s phone\"", QProcess::ReadOnly);
p->waitForFinished();

/* Read without conversion: */
qDebug() << p->readAllStandardOutput();
// "Sam\xE2\x80\x99s phone\n"

/* Read with conversion: */
qDebug() << QString::fromLocal8Bit(p->readAllStandardOutput());
// "Sam’s phone\n"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM