2017-05-08 37 views
1

我有这样转换' oct`到`c​​har` Qt中

QString result ("very much text\\374more Text"); 

一个串和backslash-int-int-int代表一个八进制数writen炭。在这种情况下,它是一个ü。我想要字符ü而不是反斜杠表示。

这就是我想:

while (result.contains('\\')) 
    if(result.length() > result.indexOf('\\') + 3) 
    { 
     bool success; 
     int i (result.mid(result.indexOf('\\') + 1, 3).toInt(&success, 8)); 
     if (success) 
     { 
      //convert i to a string 
      QString myStringOfBits ("\\u" + QString::number(i, 16)); 
      //QChar c = myStringOfBits.toUtf8(); 
      //qDebug() << c; 
     } 
    } 

我是小白,我知道

+0

编译时,你的文本不包含'\\'字符。你的编译器将'\ 374'翻译成相应的字符 – chtz

+0

qDebug给我打印'Pfad \ f \ 374r \ Ex-gesch \ 374tzte \ Dokumente'。我可以使用空格替换'\'但不是八进制字符 – Michael1248

+0

尝试['QString :: fromLatin1(“非常多文本\ 374更多文本”)](http://doc.qt.io/qt-4.8/qstring .html#fromLatin1) – chtz

回答

0

比方说,我们有一个结果字符串:

QString result ("Ordner mit \\246 und \\214"); //its: "Ordner mit ö and Ö" 

有一个解决方案:

result = QString::fromLatin1("Ordner mit \\246 und \\214"); 

,但你不能把一个变量。如果你想要把一个变量可能使用(char)(decimal)octal其字符等效:

while (result.contains("\\ ")) //replace spaces 
    result = result.replace("\\ ", " "); 
while (result.contains('\\')) //replace special characters 
    if(result.length() > result.indexOf('\\') + 3) 
    { 
     bool success; 
     int a (result.mid(result.indexOf('\\') + 1, 3).toInt(&success, 8)); //get the octal number as decimal 
     //qDebug() << a; //print octal number 
     //qDebug() << (char)a; //qDebug() will print "" because it can't handle special characters 
     if (success) 
     { 
      result = result.mid(0, result.indexOf('\\')) + 
        (char)a + //replace the special character with the char equivalent 
        result.mid(result.indexOf('\\') + 4); 
     } 

    } 

qDebug()不会显示特殊字符,但GUI的功能:

Ordner mit \246 und \214

所以它的工作原理:)谢谢大家

0

Qt中所有的代码应该是默认UTF8,所以你可以只把U中的字符串中。

+0

'\ u00FC'会给我utf8中的字符,不是吗? – Michael1248