Cleanup a QString from garbage even if it starts with a null char

91 views Asked by At

Using the QtOpcUa library I read a "string" from a PLC. The retrieved data is a QVariant:

void MyClass::opcua_valueChanged(QString key, QVariant value)
{
    qDebug() << value;
}

here a real output:

QVariant(QString, "\u0000\u0000\u0000\u0000\u000E@��\u0005���\u0010�M�X���")

Since it starts with a \0 char, I expect it is an empty string. But if I do something like:

QString s = value.toString().trimmed(); 
qDebug() << s;

I get:

"\u0000\u0000\u0000\u0000\u000E@��\u0005���\u0010�M�X���"

instead of: "". What I have to do in order to get the "real" empty string, since it starts with a null char?

1

There are 1 answers

2
Mark On

Based upon the comments received I solved in this way:

if (s.startsWith('\0')) s = "";