简体   繁体   English

将带有 UTF-8(十六进制)的 QString 转换为 QString

[英]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" .基本上,我有一个类似"Sam\xe2\x80\x99s phone"的字符串,我想将其转换为"Sam's phone"

What is the easiest way to do that in Qt or C++?在 Qt 或 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?我可以遍历每个字符并查找\x ,并将所有字符转换为十六进制值(每个\x之后的两位数字除外),然后将其转换为字符串,但有更好的方法吗?

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.我认为问题出在您阅读标准 output 的方式上。 Indeed, your standard output has a poor chance to be utf-8.实际上,您的标准 output 成为 utf-8 的机会很小。
I tried to reproduce the same behavior with echo :我试图用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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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