std::string to QString can't convert properly
-
wrote on 23 May 2020, 06:58 last edited by
Hello Experts
i am getting problem with std::string and QString
My Data Like This :
std::string Data = "Z¾xÞ?î?é?ðuÙçzá?æ?ö?èæYÆV½MÃT¾T¿M³";
Now i want to convert this string to QString But did't get the proper result
i am tried following ways to covert std:::string to QStringqDebug() << QString::fromStdString (Data) << endl; qDebug() << QString::fromStdString (Data.c_str()) << endl; qDebug() << QString::fromUtf8 (Data.c_str()) << endl; qDebug() << QString::fromLocal8Bit (Data.c_str()) << endl; qDebug() << QString(Data.c_str()).toLocal8Bit().constData() << endl;
i did't get complete conversion using this ways
if you know any other way to convert string properly then please Help me....
Please help me to solve this problem
-
@Ketan__Patel__0011 said in std::string to QString can't convert properly:
Please help me to solve this problem
You have to know which encoding the std::string is and use the correct conversion with the help of e.g. QTextCodec.
-
wrote on 23 May 2020, 07:18 last edited by
I suppose your cpp file is saved in UTF8, then the first one is the right way to go.
But some characters cannot be printed by QDebug (I don't know why).
You can show the text in a text edit or something else, and you'll see the result is right.ui->textEdit->setPlainText(QString::fromStdString (Data));
-
I suppose your cpp file is saved in UTF8, then the first one is the right way to go.
But some characters cannot be printed by QDebug (I don't know why).
You can show the text in a text edit or something else, and you'll see the result is right.ui->textEdit->setPlainText(QString::fromStdString (Data));
@Bonnie said in std::string to QString can't convert properly:
But some characters cannot be printed by QDebug (I don't know why).
They can, when the console supports utf-8 (windows cmd.exe can not)
-
I suppose your cpp file is saved in UTF8, then the first one is the right way to go.
But some characters cannot be printed by QDebug (I don't know why).
You can show the text in a text edit or something else, and you'll see the result is right.ui->textEdit->setPlainText(QString::fromStdString (Data));
wrote on 23 May 2020, 07:50 last edited by -
wrote on 23 May 2020, 08:30 last edited by
@Ketan__Patel__0011 said in std::string to QString can't convert properly:
But it is not working
What is not working? Did you read and act on each of @Bonnie's & @Christian-Ehrlicher's observations about Windows/codecs/
qDebug()
/QTextEdit
? Do you want to tell people what is not working, or do you think it's better if they guess? -
@Ketan__Patel__0011 said in std::string to QString can't convert properly:
But it is not working
What is not working? Did you read and act on each of @Bonnie's & @Christian-Ehrlicher's observations about Windows/codecs/
qDebug()
/QTextEdit
? Do you want to tell people what is not working, or do you think it's better if they guess?wrote on 23 May 2020, 09:03 last edited byYes i was read about windows / codecs / qDebug() / QTextEdit
And you misunderstood about my replyand If i used this way
qDebug() << QString::fromLocal8Bit (Data.c_str()) << endl;
Then i get most of data but i did't get all character same as std::string
-
Yes i was read about windows / codecs / qDebug() / QTextEdit
And you misunderstood about my replyand If i used this way
qDebug() << QString::fromLocal8Bit (Data.c_str()) << endl;
Then i get most of data but i did't get all character same as std::string
wrote on 23 May 2020, 09:30 last edited by JonB@Ketan__Patel__0011
But @Bonnie already told you:But some characters cannot be printed by QDebug (I don't know why).
(which is true), and he said:
You can show the text in a text edit or something else, and you'll see the result is right.
ui->textEdit->setPlainText(QString::fromStdString (Data));
So why try
qDebug()
output again, when he has made this suggestion for you to at least give a go?but i did't get all character same as std::string
If that's really the case once you have tried
QTextEdit
, then check what the length is of thestd::string
vs that of the convertedQString
. -
@Ketan__Patel__0011
But @Bonnie already told you:But some characters cannot be printed by QDebug (I don't know why).
(which is true), and he said:
You can show the text in a text edit or something else, and you'll see the result is right.
ui->textEdit->setPlainText(QString::fromStdString (Data));
So why try
qDebug()
output again, when he has made this suggestion for you to at least give a go?but i did't get all character same as std::string
If that's really the case once you have tried
QTextEdit
, then check what the length is of thestd::string
vs that of the convertedQString
.wrote on 23 May 2020, 09:45 last edited byi am use this line in my code
ui->textEdit->setPlainText(QString::fromStdString(Data));
and you can see output
and my actual string data string is :
std::string Data = "Z¾xÞ?î?é?ðuÙçzá?æ?ö?èæYÆV½MÃT¾T¿M³";
-
@Ketan__Patel__0011 said in std::string to QString can't convert properly:
and my actual string data string is :
again: which encoding is this string?
-
@Ketan__Patel__0011 said in std::string to QString can't convert properly:
and my actual string data string is :
again: which encoding is this string?
wrote on 23 May 2020, 09:56 last edited byi generate this string using my encryption algorithm and i am using some high logic for it
Like Multiple Looping Statements And Leader Conditions
-
i generate this string using my encryption algorithm and i am using some high logic for it
Like Multiple Looping Statements And Leader Conditions
wrote on 23 May 2020, 10:14 last edited by Bonnie@Ketan__Patel__0011 I think maybe your cpp file is not saved in UTF8...
But never mind, since the string is just an output of encryption algorithm...There'll be characters that even UTF8 cannot handle.
You should know that we usually don't save that kind of string (or we should call it raw data) directly, because it is not readable / printable.
We encode the raw data to hex / base64 string, and use that to save / transfer to others.
And when we need the raw data, we decode from that hex / base64 string.
If you don't want to save / transfer / make it readable, just want to get rid of a std::string and use Qt's class to store it, then you should use QByteArray instead of QString.
So there won't be any option about "what codec should be used to convert the string" since it is not a really string! -
i generate this string using my encryption algorithm and i am using some high logic for it
Like Multiple Looping Statements And Leader Conditions
wrote on 23 May 2020, 10:25 last edited by@Ketan__Patel__0011
Additional to @Bonnie's latest.i generate this string using my encryption algorithm
So should it not be a byte array/vector rather than a string?
-
@Ketan__Patel__0011
Additional to @Bonnie's latest.i generate this string using my encryption algorithm
So should it not be a byte array/vector rather than a string?
wrote on 23 May 2020, 11:59 last edited byThanks For Reply
ByteArray Or Vector Is good concept but it's is not good for sensitive data
and even it is not good for any encrypted data or any sensitive data
And Otherthing is am able to write this data in any text file
but when i try to save this string in my MYSQL Database that time it is can't store
with complete and proper data some characters are changesPlease Help me to solve this problem
This problem spoiled my whole day
-
Thanks For Reply
ByteArray Or Vector Is good concept but it's is not good for sensitive data
and even it is not good for any encrypted data or any sensitive data
And Otherthing is am able to write this data in any text file
but when i try to save this string in my MYSQL Database that time it is can't store
with complete and proper data some characters are changesPlease Help me to solve this problem
This problem spoiled my whole day
wrote on 23 May 2020, 12:05 last edited by@Ketan__Patel__0011 said in std::string to QString can't convert properly:
ByteArray Or Vector Is good concept but it's is not good for sensitive data
and even it is not good for any encrypted data or any sensitive dataNo idea what you are talking about here. I never said you should not encrypt. I asked whether the result of the encryption isn't a byte array rather than a string as you say it is. It looks like bytes and not characters to me. But if you say it's text that's up to you.
And Otherthing is am able to write this data in any text file
Regardless of encoding/codec?
but when i try to save this string in my MYSQL Database that time it is can't store
with complete and proper data some characters are changesI wonder if that's because it's bytes and not text?
-
Thanks For Reply
ByteArray Or Vector Is good concept but it's is not good for sensitive data
and even it is not good for any encrypted data or any sensitive data
And Otherthing is am able to write this data in any text file
but when i try to save this string in my MYSQL Database that time it is can't store
with complete and proper data some characters are changesPlease Help me to solve this problem
This problem spoiled my whole day
wrote on 23 May 2020, 12:09 last edited by Bonnie@Ketan__Patel__0011 I don't think anyone can / want to help you if you don't listen to anyone.
Here I'll tell you what I usually do to store encrypted data to database.
Option 1: I use QByteArray to store the encrypted data, and then save it to database as BLOB.
Option 2: I convert it to hex / base64 string, and then save it to database as string.
That's over. I'll not reply this post anymore. -
@Ketan__Patel__0011 said in std::string to QString can't convert properly:
ByteArray Or Vector Is good concept but it's is not good for sensitive data
and even it is not good for any encrypted data or any sensitive dataNo idea what you are talking about here. I never said you should not encrypt. I asked whether the result of the encryption isn't a byte array rather than a string as you say it is. It looks like bytes and not characters to me. But if you say it's text that's up to you.
And Otherthing is am able to write this data in any text file
Regardless of encoding/codec?
but when i try to save this string in my MYSQL Database that time it is can't store
with complete and proper data some characters are changesI wonder if that's because it's bytes and not text?
wrote on 23 May 2020, 12:19 last edited by Ketan__Patel__0011my Whole Process
1 -> define a normal string like:
std::string data="Welcome For Process";
2 -> Applying my encryption algorithm on this string
3 -> then store encrypted string into my file
4 -> Now for Backup i am try to save my encrypted string in MYSQL DatabaseBut i can't store my encrypted string properly, some character are changed
otherthing is when open my encrypted string file in notepad
then it show it is ANSI encodingPlease help me to solve this problem
Again My Major problem is
i want to store my encrypted string in MYSQL Database -
my Whole Process
1 -> define a normal string like:
std::string data="Welcome For Process";
2 -> Applying my encryption algorithm on this string
3 -> then store encrypted string into my file
4 -> Now for Backup i am try to save my encrypted string in MYSQL DatabaseBut i can't store my encrypted string properly, some character are changed
otherthing is when open my encrypted string file in notepad
then it show it is ANSI encodingPlease help me to solve this problem
Again My Major problem is
i want to store my encrypted string in MYSQL Databasewrote on 23 May 2020, 12:23 last edited by@Ketan__Patel__0011 said in std::string to QString can't convert properly:
2 -> Applying my encryption algorithm on this string
And how does your encryption algorithm work? Is it really producing text characters from text characters, or is it actually manipulating and producing bytes?
otherthis is when open my encrypted string file in notepad
then it show it is ANSI encodingNot sure that tells you anything from Notepad, but I'm not certain.
Please help me to solve this problem
So you keep saying.
i want to store my encrypted string in MYSQL Database
Yes, we understand this.
@Bonnie's two options are indeed the normal way to save any encrypted data to a database.
-
my Whole Process
1 -> define a normal string like:
std::string data="Welcome For Process";
2 -> Applying my encryption algorithm on this string
3 -> then store encrypted string into my file
4 -> Now for Backup i am try to save my encrypted string in MYSQL DatabaseBut i can't store my encrypted string properly, some character are changed
otherthing is when open my encrypted string file in notepad
then it show it is ANSI encodingPlease help me to solve this problem
Again My Major problem is
i want to store my encrypted string in MYSQL Database@Ketan__Patel__0011
Hi
Since its not unicode or UTF8 or real text at all you cannot just store it in the DB as it expects it to be valid text
with valid encoding.So do as @Bonnie says and store them in a blop via QByteArray.
Like this sample storing images.
https://wiki.qt.io/How_to_Store_and_Retrieve_Image_on_SQLiteNote, the type used for creating the table
query.exec( "CREATE TABLE IF NOT EXISTS imgTable ( filename TEXT, imagedata BLOB )" );Then it won't alter your data and it should work.
Your output from the encryption is no longer valid text. Treat it as a binary thing and
use QByteArray in case it might add a zero in there. std::string and QString is not ok for this sort of data
in many cases.
1/19