QString::fromStdString/toStdString convertion error
-
wrote on 15 Nov 2019, 08:27 last edited by
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"
Can someone explain what is going on. Maybe there is some explanation of this behavior.
-
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.
@farafonov-alexey
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
-
wrote on 9 Jan 2020, 11:30 last edited by
Probably https://github.com/semlanik/qtprotobuf will be useful for you