converting `\oct` to `char` in Qt

Michael1248

I have a string like

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

and the backslash-int-int-int represents a char writen in an octal number. In this case it is a ü. I want to have the char ü instead of the backslash representation.

That's what I tried:

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

I'm a noob, I know that

Michael1248

Let's say we have a result string:

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

there is a solution with:

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

but you can't put in a variable. If you want to put in a variable you might use (char) to the (decimal)octal to its char equivalent:

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() won't display special characters but GUI does:

Ordner mit \246 und \214

so it works :) thanks to all of you

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TOP Ranking

HotTag

Archive