QString::fromStdString/toStdString convertion error
-
Greetings!
The problem is similir to:
Re: QString::fromStdString/toStdString wrong behavior.We are using Qt version 5.13.0 with prebuilt components for MinGW 7.3.0 64 bit. Also we use Protobuf 3.9.0 for serialization.
There are some issues with the code below. Theserialized
string is protobuf SerializeAsString() value.std::string serialized = "\n&{0f735d28-2cc8-4696-87f5-e57dab1976a4}\022\003\061\061\061*\v0:COM6:COM70\001B\004COM1H\001P\372\001" auto q_value = QString::fromStdString(serialized); auto serialized_bug = q_value.toStdString(); // here serialized_bug = "\n&{0f735d28-2cc8-4696-87f5-e57dab1976a4}\022\003\061\061\061*\v0:COM6:COM70\001B\004COM1H\001P\357\277\275\001"
As you can see
serialized != serialized_bug
there is the difference in the end:1:"\n&{0f735d28-2cc8-4696-87f5-e57dab1976a4}\022\003\061\061\061*\v0:COM6:COM70\001B\004COM1H\001P\372\001" 2:"\n&{0f735d28-2cc8-4696-87f5-e57dab1976a4}\022\003\061\061\061*\v0:COM6:COM70\001B\004COM1H\001P\357\277\275\001"
Here is screenshot:
Can someone explain what is going on. Maybe there is some explanation of this behavior.
-
QString
is an unicode UTF-16 string and cannot store anything but (localized) text.In your serialized data you have non-text control characters that cannot be stored in a
QString
.Solution: use
QByteArray
instead.Regards
-
Probably https://github.com/semlanik/qtprotobuf will be useful for you