C++ 将Qt中的“\oct”转换为“char”

C++ 将Qt中的“\oct”转换为“char”,c++,qt,backslash,qstring,C++,Qt,Backslash,Qstring,我有一根像 QString result ("very much text\\374more Text"); 反斜杠int表示以八进制数写入的字符。在这种情况下,它是一个ü。我希望使用charü而不是反斜杠表示 这就是我尝试的: while (result.contains('\\')) if(result.length() > result.indexOf('\\') + 3) { bool success; int i (result.

我有一根像

QString result ("very much text\\374more Text");
反斜杠int
表示以八进制数写入的字符。在这种情况下,它是一个
ü
。我希望使用char
ü
而不是反斜杠表示

这就是我尝试的:

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;
        }
    }
while(result.contains('\\'))
if(result.length()>result.indexOf('\\')+3)
{
成功;
inti(result.mid(result.indexOf('\\')+1,3.toInt(&success,8));
如果(成功)
{
//将i转换为字符串
QString myStringOfBits(“\\u”+QString::number(i,16));
//QChar c=myStringOfBits.toUtf8();

//qDebug()默认情况下,qt中的所有代码都应该是utf8,因此您可以将ü放入字符串。

默认情况下,qt中的所有代码都应该是utf8,因此您可以将ü放入字符串。

假设我们有一个结果字符串:

QString result ("Ordner mit \\246 und \\214"); //its: "Ordner mit ö and Ö"
有一个解决方案:

result = QString::fromLatin1("Ordner mit \\246 und \\214");
但不能输入变量。如果要输入变量,可以使用
(char)
(十进制)八进制
到其对应的字符:

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);
        }

    }
while(result.contains(\\”)//替换空格
结果=结果。替换(“\\”,“”);
while(result.contains('\\')//替换特殊字符
if(result.length()>result.indexOf('\\')+3)
{
成功;
int a(result.mid(result.indexOf('\\'))+1,3.toInt(&success,8));//将八进制数作为十进制数

//qDebug()假设我们有一个结果字符串:

QString result ("Ordner mit \\246 und \\214"); //its: "Ordner mit ö and Ö"
有一个解决方案:

result = QString::fromLatin1("Ordner mit \\246 und \\214");
但不能输入变量。如果要输入变量,可以使用
(char)
(十进制)八进制
到其对应的字符:

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);
        }

    }
while(result.contains(\\”)//替换空格
结果=结果。替换(“\\”,“”);
while(result.contains('\\')//替换特殊字符
if(result.length()>result.indexOf('\\')+3)
{
成功;
int a(result.mid(result.indexOf('\\'))+1,3.toInt(&success,8));//将八进制数作为十进制数

//qDebug()编译时,文本不包含
\\
字符。编译器将
\374
转换为相应的字符Qdebug prints me
Pfad\f\374r\Ex gesch\374tzte\Dokumente
。我可以用空格替换
\
,但不能用不太好的八进制字符替换
\374
在qDebug()中像没有角色一样分层.第二:在这种情况下,我不能使用
QString
变量。sry@chtzsry,忘记了双反斜杠。编译时,文本不包含
\\
字符。编译器将
\374
转换为相应的字符Qdebug prints me
Pfad\f\374r\Ex gesch\374tzte\Dokumente
。我可以替换
\
带有空格,但不是八进制字符,效果不太好。
\374
在qDebug()中显示为无字符.第二:在这种情况下,我不能使用
QString
变量。sry@chtzsry,忘记了双反斜杠。
\u00FC
会给我utf8中的字符,不是吗?
\u00FC
会给我utf8中的字符,不是吗?